package cn.com.countrygarden.bi.main.service.businessCenterSubmission.Impl;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.businessCenterSubmission.BusinessCenterSubmissionMapper;
import cn.com.countrygarden.bi.main.dao.dup.TbHsprContractMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbHsprCommunityMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbSysOrganMapper;
import cn.com.countrygarden.bi.main.dto.businessCenterSubmission.SumSheetDTO;

import cn.com.countrygarden.bi.main.model.twerp.ErpCommunity;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysOrgan;
import cn.com.countrygarden.bi.main.service.businessCenterSubmission.BusinessCenterSubmissionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 *
 * </pre>
 *
 * @author wxy
 * @version 1.00.00
 * <p>
 * <p>
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 */
@Service
public class businessCenterSubmissionServiceImpl implements BusinessCenterSubmissionService {
    @Autowired
    private BusinessCenterSubmissionMapper mapper;
    @Autowired
    private TbSysOrganMapper organMapper;
    @Autowired
    private TbHsprCommunityMapper communityMapper;
    /**
     * 获取区域列表
     *
     * @return
     */
    @Override
    public Result getOrganList() {
        List<TbSysOrgan> organs = organMapper.findAllActive();
        return new Result().setData(organs);
    }

    /**
     * 根据OrganCode获取项目列表
     *
     * @return
     */
    @Override
    public Result getCommListByOrganCode(String organCode) {
        List<ErpCommunity> erpCommunities = communityMapper.slectErpCommunityByOrganCode(organCode);
        return new Result().setData(erpCommunities);
    }

    /**
     * 获取项目列表
     *
     * @return
     */
    @Override
    public Result getCommList() {
        List<ErpCommunity> communityList = communityMapper.findAllBaseComm();
        return new Result().setData(communityList);
    }

    /**
     * 获取未完成征询原因类型
     *
     * @return
     */
    @Override
    public Result getUnfinishedCause() {
        List<Map<String, String>> unfinishedCause = mapper.getUnfinishedCause();
        return new Result().setData(unfinishedCause);
    }

    /**
     * 查询汇总数据列表
     *
     * @param organCode
     * @param commId
     * @param branchName
     * @param isPrd
     * @param isDouble
     * @param unfinishedCause
     * @param isDoubleSixMonth
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Result getSumSheetList(String userCode,Integer pageNum, Integer pageSize,String id,String organCode, String commId, String branchName, String isPrd, String isDouble, String unfinishedCause, String isDoubleSixMonth, String startTime, String endTime) {
        TbSysOrgan code = organMapper.findByUserCode(userCode);
        //分页
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }

        //获取列表
        List<SumSheetDTO> list = null;
        String organ = code.getOrganCode();
        String commID = code.getCommID();

        if (commID.equals("0")&& !organ.equals("01")){
            list = mapper.getSumSheetList(code.getOrganCode(), commId, id, branchName, isPrd, isDouble, unfinishedCause, isDoubleSixMonth, startTime, endTime);
        }else if (organ.equals("01") && commID.equals("0")){
            list = mapper.getSumSheetList(organCode, commId, id, branchName, isPrd, isDouble, unfinishedCause, isDoubleSixMonth, startTime, endTime);
        }else{
            list = mapper.getSumSheetList(code.getOrganCode(),code.getCommID(), id, branchName, isPrd, isDouble, unfinishedCause, isDoubleSixMonth, startTime, endTime);
        }
        if (list == null){
            list = new ArrayList<>();
        }
        PageInfo info = new PageInfo(list);
        return new Result().setData(info);
    }

    @Override
    public Result getUserRole(String userCode) {
        Map<String,String> map = new HashMap<>();
        TbSysOrgan code = organMapper.findByUserCode(userCode);
        ErpCommunity erpCommunity= null;
        try {
            if (!code.getCommID().equals("0")){
                List<ErpCommunity> comm = communityMapper.findBaseCommByCommId(code.getCommID());
                erpCommunity = comm.get(0);
                map.put("OrganCode",erpCommunity.getOrganCode());
                map.put("OrganName",erpCommunity.getOrganName());
                map.put("CommId",erpCommunity.getCommId());
                map.put("CommName",erpCommunity.getCommName());
            }else{
                map.put("OrganCode",code.getOrganCode());
                map.put("OrganName",code.getOrganName());
                map.put("CommId",code.getCommID());
                map.put("CommName","");
            }
        }catch (NullPointerException e){
            return new Result().setData("此用户对应项目不存在");
        }

        String roleName = "";
            if (code.getCommID().equals("0")&&!code.getOrganCode().equals("01")) {
                roleName = "submission-organ";
            }else if (code.getOrganCode().equals("01")&&code.getCommID().equals("0")){
                roleName = "submission-admin";
            }else{
                roleName = "submission-comm";
            }
        map.put("roleName",roleName);
        return new Result().setData(map);
    }

    /**
     * 新增汇总数据
     *
     * @param dto
     * @param userCode
     * @return
     */
    @Override
    public Result addSumSheet(SumSheetDTO dto, String userCode) {
        if(dto.getUnfinishedCauseText() == null){
            dto.setUnfinishedCauseText("");
        }
        if(dto.getIsDoubleSixMonthFlaseCause() == null){
            dto.setIsDoubleSixMonth("");
        }
        if(dto.getRemark() == null){
            dto.setRemark("");
        }
        if (dto.getRemark().equals("null")||dto.getRemark().equals("undefined")){
            dto.setRemark("");
        }
        mapper.addSumSheet(dto, userCode);
        return new Result().setData("新增成功");
    }

    /**
     * 修改汇总数据
     *
     * @param dto
     * @param userCode
     * @return
     */
    @Override
    public Result updateSumSheet(SumSheetDTO dto, String userCode) {
        if (dto.getRemark().equals("null")||dto.getRemark().equals("undefined")){
            dto.setRemark("");
        }
        mapper.updateSumSheet(dto, userCode);
        return new Result().setData("修改成功");
    }

    /**
     * 删除汇总数据
     *
     * @param id
     * @param userCode
     * @return
     */
    @Override
    public Result deleteSumSheet(String id, String userCode) {
        mapper.deleteSumSheet(id, userCode);
        return new Result().setData("删除成功");
    }


}
