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

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.xmgl.modules.basicdata.dto.GovernmentDto;
import com.zhgd.xmgl.modules.basicdata.entity.Government;
import com.zhgd.xmgl.modules.basicdata.entity.Project;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.mapper.GovernmentMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.ProjectMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemUserMapper;
import com.zhgd.xmgl.modules.basicdata.service.IGovernmentService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemDictDataService;
import com.zhgd.xmgl.modules.basicdata.vo.GovernmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Description: 政务信息
 * @author： pds
 * @date： 2023-02-15
 * @version： V1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GovernmentServiceImpl extends ServiceImpl<GovernmentMapper, Government> implements IGovernmentService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Autowired
    private GovernmentMapper governmentMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ISystemDictDataService systemDictDataService;

    @Override
    public IPage<Government> selectPage(Page page, Wrapper<Government> wrapper) {
        Page<Government> pageList = this.page(page, wrapper);
        List<Project> projectList = new ArrayList<>();
        if (pageList.getRecords().size() > 0) {
            projectList = projectMapper.selectList(Wrappers.<Project>lambdaQuery()
                    .in(Project::getGovernmentSn, pageList.getRecords().stream().map(g -> g.getGovernmentSn()).collect(Collectors.toList())));
        }
        for (Government government : pageList.getRecords()) {
            if (government.getExpireTime() != null) {
                government.setDiffDay((int) DateUtil.between(new Date(), government.getExpireTime(), DateUnit.DAY));
            }
            government.setInstallProject(projectList.stream().filter(p -> p.getGovernmentSn().equals(government.getGovernmentSn())).count());
        }
        return pageList;
    }

    @Override
    public int saveInfo(GovernmentVo governmentVo) {
        String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        Government government = new Government();
        government.setGovernmentName(governmentVo.getGovernmentName());
        government.setGovernmentSn(uuid);
        government.setGovernmentTel(governmentVo.getGovernmentTel());
        government.setCreateTime(new Date());
        this.save(government);
        SystemUser systemUser = new SystemUser();
        systemUser.setAccount(governmentVo.getAccount());
        systemUser.setShowPassword(governmentVo.getPassword());
        systemUser.setCreateTime(new Date());
        systemUser.setSn(government.getGovernmentSn());
        systemUser.setIsManager(true);
        systemUser.setAccountType(2);
        return systemUserMapper.insert(systemUser);
    }

    @Override
    public int updateInfo(GovernmentVo governmentVo) {
        Government government = governmentMapper.selectById(governmentVo.getGovernmentId());
        government.setGovernmentName(governmentVo.getGovernmentName());
        government.setGovernmentTel(governmentVo.getGovernmentTel());
        government.setAuthProject(governmentVo.getAuthProject());
        government.setState(governmentVo.getState());
        government.setExpireTime(governmentVo.getExpireTime());
        government.setIsShowLockModule(governmentVo.getIsShowLockModule());
        government.setModuleStyle(governmentVo.getModuleStyle());
        this.updateById(government);
        SystemUser systemUser = new SystemUser();
        systemUser.setAccount(governmentVo.getAccount());
        systemUser.setShowPassword(governmentVo.getPassword());
        return systemUserMapper.update(systemUser, Wrappers.<SystemUser>lambdaQuery()
                .eq(SystemUser::getIsManager, true).eq(SystemUser::getSn, government.getGovernmentSn()));
    }

    @Override
    public GovernmentDto getDetailById(Government government) {
        GovernmentDto governmentDto = new GovernmentDto();
        BeanUtils.copyProperties(government, governmentDto);
        SystemUser systemUser = systemUserMapper.selectOne(Wrappers.<SystemUser>lambdaQuery()
                .eq(SystemUser::getSn, government.getGovernmentSn())
                .eq(SystemUser::getIsManager, true));
        governmentDto.setAccount(systemUser.getAccount());
        governmentDto.setPassword(systemUser.getPassword());
        return governmentDto;
    }
}
