package com.liquidnet.service.adam.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.adam.dto.AdamEntersParam;
import com.liquidnet.service.adam.dto.vo.AdamEntersVo;
import com.liquidnet.service.adam.entity.AdamEnters;
import com.liquidnet.service.adam.service.IAdamEntersService;
import com.liquidnet.service.adam.service.IAdamRdmService;
import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.ResponseDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 入场人 前端控制器
 * </p>
 *
 * @author liquidnet
 * @since 2021-04-28
 */
@ApiSupport(order = 10040)
@Api(tags = "入场人")
@Slf4j
@Validated
@RestController
@RequestMapping("enters")
public class AdamEntersController {
    @Autowired
    IAdamRdmService adamRdmService;
    @Autowired
    IAdamEntersService adamEntersService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "添加入场人")
    @PostMapping("add")
    public ResponseDto<String> add(@RequestBody @Valid AdamEntersParam parameter) {
        String currentUid = CurrentUtil.getCurrentUid();

        AdamEnters addInfo = new AdamEnters();
        BeanUtils.copyProperties(parameter, addInfo);
        addInfo.setUid(currentUid);
        addInfo.setEntersId(String.valueOf(IDGenerator.nextSnowId()));
        addInfo.setCreatedAt(LocalDateTime.now());
        addInfo.setState(1);

        adamEntersService.add(addInfo);

        return ResponseDto.success(addInfo.getEntersId());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "入场人列表")
    @GetMapping("list")
    public ResponseDto<List<AdamEntersVo>> list() {
        String currentUid = CurrentUtil.getCurrentUid();

        List<AdamEntersVo> voList = new ArrayList<>();
        adamRdmService.getEntersByUid(currentUid).forEach(i -> {
            voList.add(AdamEntersVo.getNew().copy(i));
        });

        return ResponseDto.success(voList);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "设置默认入场人")
    @PostMapping("def/{entersId}")
    public ResponseDto<Object> def(@NotBlank @PathVariable String entersId) {
        String currentUid = CurrentUtil.getCurrentUid();
        AdamEnters enters = adamRdmService.getEntersByUidEntersId(currentUid, entersId);
        if (null == enters) return ResponseDto.failure(ErrorMapping.get("10105"));

        if (!enters.getIsDefault()) {
            adamEntersService.def(currentUid, entersId);
        }
        return ResponseDto.success();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "编辑入场人")
    @PostMapping("edit")
    public ResponseDto<Object> edit(@RequestBody @Valid AdamEntersParam parameter) {
        if (StringUtils.isBlank(parameter.getEntersId())) {
            return ResponseDto.failure(ErrorMapping.get("10015"));
        }
        String currentUid = CurrentUtil.getCurrentUid();

        AdamEnters enters = adamRdmService.getEntersByUidEntersId(currentUid, parameter.getEntersId());
        if (null == enters) return ResponseDto.failure(ErrorMapping.get("10105"));

        AdamEnters updateInfo = new AdamEnters();
        BeanUtils.copyProperties(parameter, updateInfo);
        updateInfo.setUpdatedAt(LocalDateTime.now());
        updateInfo.setUid(currentUid);
        updateInfo.setState(1);

        adamEntersService.edit(updateInfo);

        return ResponseDto.success();
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除入场人")
    @PostMapping("del/{entersId}")
    public ResponseDto<Object> del(@NotBlank @PathVariable String entersId) {
        String currentUid = CurrentUtil.getCurrentUid();

        AdamEnters enters = adamRdmService.getEntersByUidEntersId(currentUid, entersId);
        if (null == enters) return ResponseDto.failure(ErrorMapping.get("10105"));

        adamEntersService.remove(currentUid, entersId);
        return ResponseDto.success();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "入场人详情")
    @PostMapping("query/{entersId}")
    public ResponseDto<AdamEntersVo> query(@NotBlank @PathVariable String entersId,
                                           @NotBlank @RequestParam(required = false) String uid) {
        AdamEnters info = adamRdmService.getEntersByUidEntersId(StringUtils.isBlank(uid) ? CurrentUtil.getCurrentUid() : uid, entersId);

        return null == info ? ResponseDto.failure(ErrorMapping.get(10105)) : ResponseDto.success(AdamEntersVo.getNew().copy(info));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取默认入场人")
    @PostMapping("query/def")
    public ResponseDto<AdamEntersVo> queryDef() {
        return ResponseDto.success(adamEntersService.queryDefault(CurrentUtil.getCurrentUid()));
    }
}
