package com.mosty.qwzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mosty.base.model.dto.qwzx.AppSearchQuery;
import com.mosty.base.model.dto.qwzx.TbQwQwbcEditDto;
import com.mosty.base.model.entity.qwzx.TbQwQwbc;
import com.mosty.base.model.query.qwzx.TbQwQwbcQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.qwzx.mapper.TbQwQwbcMapper;
import com.mosty.qwzx.remote.TbBaseAdaptRemoteService;
import com.mosty.qwzx.service.TbQwQwbcService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 勤务班次表- 服务实现类
 * </p>
 *
 * @author zengbo
 * @since 2022-06-27
 */
@AllArgsConstructor
@Service
public class TbQwQwbcServiceImpl implements TbQwQwbcService {

    private final TbQwQwbcMapper tbQwQwbcMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public int addQwbc(TbQwQwbc qwbc) {
        qwbc.setId(UUIDGenerator.getUUID());
        qwbc.setXtSjly("1");
        if (StringUtils.isBlank(qwbc.getSsbmdm())) throw new BusinessException("请选择部门！！");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(qwbc.getSsbmdm());
        if (dept != null) {
            qwbc.setSsbm(dept.getDeptname());
            qwbc.setSsbmid(dept.getDeptid());
            qwbc.setSsbmdm(dept.getDeptcode());
            qwbc.setSsxgajdm(dept.getFxjcode());
            qwbc.setSsxgajid(dept.getFxjid());
            qwbc.setSsxgaj(dept.getFxjname());
            qwbc.setSssgaj(dept.getDszname());
            qwbc.setSssgajdm(dept.getDszcode());
            qwbc.setSssgajid(dept.getDszid());
        }
//        UserInfo user = UserInfoManager.get();
//        qwbc.setSsbm(user.getDeptName());
//        qwbc.setSsbmid(String.valueOf(user.getDeptId()));
//        qwbc.setSsbmdm(user.getDeptCode());
//        qwbc.setSsxgaj(user.getFxjDeptName());
//        qwbc.setSsxgajid(String.valueOf(user.getFxjDeptId()));
//        qwbc.setSsxgajdm(user.getFxjDeptCode());
//        qwbc.setSssgaj(user.getDszDeptName());
//        qwbc.setSssgajid(String.valueOf(user.getDszDeptId()));
//        qwbc.setSssgajdm(user.getDszDeptCode());
        return tbQwQwbcMapper.insert(qwbc);
    }

    @Override
    public IPage<TbQwQwbc> getQwbc(TbQwQwbcQuery qwbc, IPage<TbQwQwbc> page) {
        QueryWrapper<TbQwQwbc> queryWrapper = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(queryWrapper, userInfo);
        qwbc.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(qwbc.getSsbmdm(), null));
        if (qwbc != null) {
            queryWrapper.lambda()
                    .like(StringUtils.isNotBlank(qwbc.getBcmc()), TbQwQwbc::getBcmc, qwbc.getBcmc())
                    .likeRight(StringUtils.isNotBlank(qwbc.getSsbmdm()), TbQwQwbc::getSsbmdm, qwbc.getSsbmdm())
                    .ge(qwbc.getKssj() != null, TbQwQwbc::getKssj, qwbc.getKssj())
                    .le(qwbc.getJssj() != null, TbQwQwbc::getJssj, qwbc.getJssj())
                    .eq(StringUtils.isNotBlank(qwbc.getPbjb()), TbQwQwbc::getPbjb, qwbc.getPbjb())
                    .eq(StringUtils.isNotBlank(qwbc.getBclx()), TbQwQwbc::getBclx, qwbc.getBclx());
        }
        return tbQwQwbcMapper.selectPage(page, queryWrapper);
    }

    @Override
    public int editQwbc(TbQwQwbcEditDto qwbc) {
        TbQwQwbc bc = new TbQwQwbc();
        BeanUtils.copyProperties(qwbc, bc);
        if (StringUtils.isBlank(qwbc.getSsbmdm())) throw new BusinessException("请选择部门！！");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(qwbc.getSsbmdm());
        if (dept != null) {
            bc.setSsbm(dept.getDeptname());
            bc.setSsbmid(dept.getDeptid());
            bc.setSsbmdm(dept.getDeptcode());
            bc.setSsxgajdm(dept.getFxjcode());
            bc.setSsxgajid(dept.getFxjid());
            bc.setSsxgaj(dept.getFxjname());
            bc.setSssgaj(dept.getDszname());
            bc.setSssgajdm(dept.getDszcode());
            bc.setSssgajid(dept.getDszid());
        }
//        UserInfo user = UserInfoManager.get();
//        qwbc.setSsbm(user.getDeptName());
//        qwbc.setSsbmid(String.valueOf(user.getDeptId()));
//        qwbc.setSsbmdm(user.getDeptCode());
//        qwbc.setSsxgaj(user.getFxjDeptName());
//        qwbc.setSsxgajid(String.valueOf(user.getFxjDeptId()));
//        qwbc.setSsxgajdm(user.getFxjDeptCode());
//        qwbc.setSssgaj(user.getDszDeptName());
//        qwbc.setSssgajid(String.valueOf(user.getDszDeptId()));
//        qwbc.setSssgajdm(user.getDszDeptCode());
        return tbQwQwbcMapper.updateById(bc);
    }

    @Override
    public boolean deleteBatch(List<String> ids) {
        if (ids.size() > 0) {
            int rs = tbQwQwbcMapper.deleteQwbc(ids);
            return rs > 0;
        }
        return true;
    }

    @Override
    public boolean deleteById(String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        int rs = tbQwQwbcMapper.deleteQwbc(ids);
        return rs > 0;
    }

    @Override
    public TbQwQwbc selectById(String id) {
        return tbQwQwbcMapper.selectById(id);
    }

    @Override
    public Page<TbQwQwbc> getBcList(AppSearchQuery dto) {
        UserInfo user = UserInfoManager.get();

        LambdaQueryWrapper<TbQwQwbc> wrapper = new LambdaQueryWrapper<TbQwQwbc>()
                .eq(StringUtils.isNotBlank(dto.getBclx()), TbQwQwbc::getBclx, dto.getBclx())
                .eq(TbQwQwbc::getXtSjzt, "1");

        if ("1".equals(dto.getType())) {
            wrapper.like(StringUtils.isNotBlank(dto.getKeyword()), TbQwQwbc::getBcmc, dto.getKeyword())
                    .eq(TbQwQwbc::getSsbmdm, String.valueOf(user.getDeptCode()));
        } else {
            String ssbm = this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null);
            wrapper.likeRight(StringUtils.isNotBlank(ssbm), TbQwQwbc::getSsbmdm, ssbm)
                    .and(StringUtils.isNotBlank(dto.getKeyword()),
                            it -> it.like(TbQwQwbc::getBcmc, dto.getKeyword())
                                    .or()
                                    .like(TbQwQwbc::getSsbm, dto.getKeyword())
                    );
        }
        return this.tbQwQwbcMapper.selectPage(new Page<>(dto.getPageCurrent(), dto.getPageSize()), wrapper);

    }
}
