package com.blueland.render.rest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blueland.bo.PositionInfoBo;
import com.blueland.bo.TucStaffEntity;
import com.blueland.constant.Constant;
import com.blueland.dao.OrgDao;
import com.blueland.dao.StaffDao;
import com.blueland.model.TUcOrg;
import com.blueland.model.TUcStaff;
import com.blueland.result.R;
import com.blueland.service.ITucOrgService;
import com.blueland.service.ITucStaffService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @auther xcy
 * @create 2022-06-07 15:38
 * 用户任岗同步接口
 */
@RestController
@RequestMapping({"/userpost"})
public class PostCodeController {

    @Autowired
    private ITucStaffService itUcStaffService;
    @Autowired
    private ITucOrgService itUcOrgService;

    @Resource
    StaffDao staffDao;

    @Resource
    OrgDao orgDao;


    @ApiOperation("主岗调岗")
    @PostMapping({"/update"})
    public R<Boolean> mainPositionChange(@RequestBody PositionInfoBo positionInfo) {
        QueryWrapper<TUcStaff> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TUcStaff::getStaffNum, positionInfo.getStaffNum());
        queryWrapper.lambda().eq(TUcStaff::getStaffPostType, Constant.MAIN);
        TUcStaff tUcStaff = new TUcStaff();
        this.initPositionInfo(tUcStaff, positionInfo);
        Integer result = this.staffDao.update(tUcStaff, queryWrapper);
        if (result > 0) {
            return R.ok(true, "成功");
        } else {
            return R.fail("主岗调岗信息不存在");
        }
    }

    @ApiOperation("新增兼岗")
    @PostMapping({""})
    public R<Boolean> addParttimePosition(@RequestBody PositionInfoBo positionInfo) {
        LambdaQueryWrapper<TUcStaff> lqw = new LambdaQueryWrapper();
        lqw.eq(TUcStaff::getStaffPostCode, positionInfo.getStaffPostCode());
        lqw.eq(TUcStaff::getStaffStatus, Constant.QY);
        List<TUcStaff> list = this.staffDao.selectList(lqw);
        if (ObjectUtil.isNotEmpty(list)) {
            TUcStaff tUcStaff = new TUcStaff();
            this.initPositionInfo(tUcStaff, positionInfo);
            this.staffDao.update(tUcStaff, lqw);
            return R.ok(true, "成功");
        } else {
            lqw = new LambdaQueryWrapper();
            lqw.eq(TUcStaff::getStaffNum, positionInfo.getStaffNum());
            lqw.eq(TUcStaff::getStaffPostType, Constant.MAIN);
            List<TUcStaff> staffList = this.staffDao.selectList(lqw);
            if (ObjectUtil.isEmpty(staffList)) {
                return R.fail("新增兼岗时主岗信息不存在");
            } else {
                TUcStaff staff = BeanUtil.copyProperties(staffList.get(0), TUcStaff.class);
                this.initPositionInfo(staff, positionInfo);
                List<TUcStaff> addList = itUcStaffService.handleStaffToPostCode(staff);
                if (ObjectUtil.isNotEmpty(addList)) {
                    for (TUcStaff tUcStaff : addList) {
                        this.initPositionInfo(tUcStaff, positionInfo);
                        tUcStaff.setStaffId(null);
                        this.staffDao.insert(tUcStaff);
                    }
                }
                return R.ok(true, "成功");
            }
        }
    }

    @ApiOperation("删除兼岗")
    @GetMapping({"/{staffPostCode}"})
    public R<Boolean> removeParttimePosition(@PathVariable String staffPostCode) {
        LambdaQueryWrapper<TUcStaff> lqw = new LambdaQueryWrapper();
        lqw.eq(TUcStaff::getStaffPostCode, staffPostCode);
        List<TUcStaff> list = this.staffDao.selectList(lqw);
        if (ObjectUtil.isNotEmpty(list)) {
            for (TUcStaff tUcStaff : list) {
                tUcStaff.setStaffStatus(Constant.JY);
                this.staffDao.update(tUcStaff, lqw);
            }
            return R.ok(true, "成功");
        } else {
            return R.fail("删除兼岗时兼岗信息不存在");
        }
    }

    private void initPositionInfo(TUcStaff tUcStaff, PositionInfoBo positionInfo) {
        LambdaQueryWrapper<TUcOrg> lqw = new LambdaQueryWrapper();
        lqw.eq(TUcOrg::getDeptId, positionInfo.getDeptCode());
        List<TUcOrg> list = this.orgDao.selectList(lqw);
        if (ObjectUtil.isNotEmpty(list)) {
            TUcOrg tUcOrg = list.get(0);
            tUcStaff.setOrgId(tUcOrg.getOrgId());
            tUcStaff.setOrgName(tUcOrg.getOrgName());
        }
        tUcStaff.setStaffPostCode(positionInfo.getStaffPostCode());
        tUcStaff.setStaffPostType(positionInfo.getStaffPostType());
        tUcStaff.setDeptId(positionInfo.getDeptCode());
        tUcStaff.setDeptName(positionInfo.getDeptName());
        tUcStaff.setPositionId(positionInfo.getPositionCode());
        tUcStaff.setPositionName(positionInfo.getPositionName());
        tUcStaff.setStaffCreator(positionInfo.getCreateUser());
        tUcStaff.setStaffCreateTime(positionInfo.getCreateTime());
        tUcStaff.setStaffNum(positionInfo.getStaffNum());
        tUcStaff.setStaffCode(positionInfo.getStaffCode());
        tUcStaff.setStaffStatus(Constant.QY);
        if (!Objects.equals(positionInfo.getStatus(), "enable")) {
            tUcStaff.setStaffStatus(Constant.JY);
        }
    }

}
