package com.zhgd.xmgl.modules.basicdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.dto.PolicyDto;
import com.zhgd.xmgl.modules.basicdata.entity.*;
import com.zhgd.xmgl.modules.basicdata.mapper.PolicyMapper;
import com.zhgd.xmgl.modules.basicdata.service.*;
import com.zhgd.xmgl.modules.basicdata.vo.PolicyVo;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import com.zhgd.xmgl.util.ParamEnum;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 政策法规
 * @author： pengj
 * @date：   2023-03-02
 * @version： V1.0
 */
@Service
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements IPolicyService {
    @Autowired
    private IAnnexFileService annexFileService;

    @Autowired
    private IGovernmentService governmentService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Override
    public Page<Policy> toMePageList(Map<String, Object> map) {
        QueryWrapper<Policy> wrapper = Wrappers.<Policy>query();
        String title = MapUtils.getString(map, "title");
        String content = MapUtils.getString(map, "content");
        Page<Policy> page = PageUtil.getPage(map);
        SecurityUser user = SecurityUtil.getUser();
        String sn = user.getSn();
        if (user.getAccountType() == 4) {
            sn = projectService.getOne(Wrappers.<Project>lambdaQuery().eq(Project::getProjectSn, user.getSn())).getGovernmentSn();
        } else if (user.getAccountType() == 3) {
            sn = enterpriseService.getOne(Wrappers.<Enterprise>lambdaQuery().eq(Enterprise::getEnterpriseSn, user.getSn())).getGovernmentSn();
        }
        wrapper.eq("p.create_by", sn);
        if (StringUtils.isNotBlank(title)) {
            wrapper.like("p.title", title);
        }
        if (StringUtils.isNotBlank(content)) {
            wrapper.like("p.content", content);
        }
        return baseMapper.toMePageList(page, wrapper);
    }

    @Override
    public PolicyDto getDetail(Policy policy) {
        PolicyDto policyDto = new PolicyDto();
        BeanUtils.copyProperties(policy, policyDto);
        Government government = governmentService.getOne(Wrappers.<Government>lambdaQuery().eq(Government::getGovernmentSn, policy.getCreateBy()));
        policyDto.setCreateByName(government.getGovernmentName());
        List<AnnexFile> annexFileList = annexFileService.list(Wrappers.<AnnexFile>lambdaQuery().eq(AnnexFile::getRelevanceId, policy.getPolicyId())
                .eq(AnnexFile::getFileType, ParamEnum.AnnexFileType.POLICY.getValue()));
        policyDto.setAnnexFileList(annexFileList);
        return policyDto;
    }

    @Override
    public boolean saveInfo(PolicyVo policyVo) {
        Policy policy = new Policy();
        BeanUtils.copyProperties(policyVo, policy);
        policy.setCreateBy(SecurityUtil.getUser().getSn());
        policy.setCreateTime(new Date());
        this.save(policy);
        return saveDetail(policyVo, policy.getPolicyId());
    }

    @Override
    public boolean updateInfo(PolicyVo policyVo) {
        Policy policy = new Policy();
        BeanUtils.copyProperties(policyVo, policy);
        this.updateById(policy);
        annexFileService.remove(Wrappers.<AnnexFile>lambdaQuery().eq(AnnexFile::getRelevanceId, policy.getPolicyId())
                .eq(AnnexFile::getFileType, ParamEnum.AnnexFileType.POLICY.getValue()));
        return saveDetail(policyVo, policy.getPolicyId());
    }

    @Override
    public boolean delInfo(Policy policy) {
        annexFileService.remove(Wrappers.<AnnexFile>lambdaQuery().eq(AnnexFile::getRelevanceId, policy.getPolicyId())
                .eq(AnnexFile::getFileType, ParamEnum.AnnexFileType.POLICY.getValue()));
        return this.removeById(policy.getPolicyId());
    }

    private boolean saveDetail(PolicyVo policyVo, Long policyId) {
        List<AnnexFile> annexFileList = policyVo.getAnnexFileList();
        annexFileList.stream().forEach(a -> {
            a.setRelevanceId(policyId.toString());
            a.setCreateTime(new Date());
            a.setFileType(ParamEnum.AnnexFileType.NOTICE.getValue());
        });
        return annexFileService.saveBatch(annexFileList);
    }
}
