package com.xintong.visualinspection.visuallnspection_qzd.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xintong.visualinspection.bean.Constant;
import com.xintong.visualinspection.bean.FeeStation;
import com.xintong.visualinspection.bean.FwqCheckOther;
import com.xintong.visualinspection.bean.FwqCheckOtherStatistics;
import com.xintong.visualinspection.bean.Item;
import com.xintong.visualinspection.bean.Organ;
import com.xintong.visualinspection.service.CheckOtherService_F;
import com.xintong.visualinspection.service.CommonService;
import com.xintong.visualinspection.visuallnspection_qzd.dao.FwqCheckOtherDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * 文件名：OtherCheckServiceImpl_F
 * 版本信息：日期：2017/4/7 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 *
 * @author wenhongquan
 * @date 2018/05/02
 */

@com.alibaba.dubbo.config.annotation.Service(
        version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class CheckOtherServiceImpl implements CheckOtherService_F {

    private static final Logger logger = LoggerFactory.getLogger(CheckOtherServiceImpl.class);

    @Autowired
    private FwqCheckOtherDao fwqCheckOtherDao;

    @Reference(version = "1.0.0",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.address}")
    private CommonService commonService;

    /**
     * 第三方暗访稽查查询(查询条件:时间,道管,服务区)
     *
     * @param page
     * @param size
     * @param fwqCheckOther
     * @return
     */
    @Override
    public Object getAllCheckOther(Integer page, Integer size, FwqCheckOther fwqCheckOther) {
        if (!(page.equals(0) && size.equals(0))) {
            PageHelper.startPage(page, size);
        }
        List<FwqCheckOther> checkOtherList = fwqCheckOtherDao.getAllCheckOther(fwqCheckOther == null ? new FwqCheckOther() : fwqCheckOther);
        Map<Long, Organ> deptMap = commonService.getDeptMap();
        Map<Long, Item> itemMap = commonService.getItemMap();
        Map<String, Constant> codeMap = commonService.getCodeMap();
        for (FwqCheckOther data : checkOtherList) {
            if (deptMap.get(data.getDept_id()) != null) {
                data.setOrgan_name(deptMap.get(data.getDept_id()).getOrganname());
            }
            if (itemMap.get(data.getCheck_item_id()) != null) {
                data.setCheck_item_name(itemMap.get(data.getCheck_item_id()).getName());
                if (itemMap.get(data.getCheck_item_id()).getParent_id() != null) {
                    if (itemMap.get(data.getCheck_item_id()).getScore() == null) {
                        data.setPcheckname(itemMap.get(data.getCheck_item_id()).getName());
                    } else {
                        data.setPcheckname(itemMap.get(itemMap.get(data.getCheck_item_id()).getParent_id()).getName());
                    }
                }
            }
            if (codeMap.get(data.getWrecker_check_type()) != null) {
                data.setWrecker_check_type_name(codeMap.get(data.getWrecker_check_type()).getCode_name());
            }
            if (codeMap.get(data.getSpecial_check_type()) != null) {
                data.setSpecial_check_type_name(codeMap.get(data.getSpecial_check_type()).getCode_name());
            }
        }
        if (!(page.equals(0) && size.equals(0))) {
            return new PageInfo<FwqCheckOther>(checkOtherList);
        }
        return checkOtherList;
    }

    /**
     * 第三方暗访稽查查询(查询条件:时间,道管,服务区),<无分页>
     *
     * @param fwqCheckOther
     * @return
     */
    private Object getAllCheckOther(FwqCheckOther fwqCheckOther) {

        List<FwqCheckOther> checkOtherList = fwqCheckOtherDao.getAllCheckOther(fwqCheckOther == null ? new FwqCheckOther() : fwqCheckOther);
        Map<Long, Organ> deptMap = commonService.getDeptMap();
        Map<Long, Item> itemMap = commonService.getItemMap();
        Map<String, Constant> codeMap = commonService.getCodeMap();
        for (FwqCheckOther data : checkOtherList) {
            if (deptMap.get(data.getDept_id()) != null) {
                data.setOrgan_name(deptMap.get(data.getDept_id()).getOrganname());
            }
            if (itemMap.get(data.getCheck_item_id()) != null) {
                data.setCheck_item_name(itemMap.get(data.getCheck_item_id()).getName());
                if (itemMap.get(data.getCheck_item_id()).getParent_id() != null) {
                    if (itemMap.get(data.getCheck_item_id()).getScore() == null) {
                        data.setPcheckname(itemMap.get(data.getCheck_item_id()).getName());
                    } else {
                        data.setPcheckname(itemMap.get(itemMap.get(data.getCheck_item_id()).getParent_id()).getName());
                    }
                }
            }
            if (codeMap.get(data.getWrecker_check_type()) != null) {
                data.setWrecker_check_type_name(codeMap.get(data.getWrecker_check_type()).getCode_name());
            }
            if (codeMap.get(data.getSpecial_check_type()) != null) {
                data.setSpecial_check_type_name(codeMap.get(data.getSpecial_check_type()).getCode_name());
            }
        }
        return checkOtherList;
    }

    /**
     * 新增稽查数据
     *
     * @param fwqOtherCheck
     */
    @Override
    public void addCheckOther(FwqCheckOther fwqOtherCheck) {
        fwqCheckOtherDao.addCheckOther(fwqOtherCheck);
    }

    @Override
    public void deleteCheckOther(Long id) {
        fwqCheckOtherDao.deleteCheckOther(id);
    }

    @Override
    public void updateCheckOther(FwqCheckOther fwqCheckOther) {
        fwqCheckOtherDao.updateCheckOther(fwqCheckOther);
    }

    @Override
    public FwqCheckOther selectCheckById(Long id) {
        FwqCheckOther fwqCheckOther = fwqCheckOtherDao.selectCheckById(id);
        Map<Long, Item> itemMap = commonService.getItemMap();
        fwqCheckOther.setCheck_item_name(itemMap.get(fwqCheckOther.getCheck_item_id()).getName());
        return fwqCheckOther;
    }

    /**
     * 统计数据
     *
     * @param fwqCheckOther
     * @return
     */
    @Override
    public Map<String, Object> getCheckOtherStatistics(FwqCheckOther fwqCheckOther) {

        Map<String, Object> datas = new HashMap<>();
        List<FwqCheckOtherStatistics> fwqCheckOtherStatisticses = new ArrayList<>();

        fwqCheckOther.setCheck_time_name(fwqCheckOther.getCheck_period());
        List<FwqCheckOther> checkOtherList = fwqCheckOtherDao.getAllCheckOther(fwqCheckOther);
        Map<Long, Organ> deptMap = commonService.getDeptMap();
        Map<Long, Item> itemMap = commonService.getItemMap();
        Map<String, Constant> codeMap = commonService.getCodeMap();
        for (FwqCheckOther data : checkOtherList) {
            if (deptMap.get(data.getDept_id()) != null) {
                data.setOrgan_name(deptMap.get(data.getDept_id()).getOrganname());
            }
            if (itemMap.get(data.getCheck_item_id()) != null) {
                data.setCheck_item_name(itemMap.get(data.getCheck_item_id()).getName());
                if (itemMap.get(data.getCheck_item_id()).getParent_id() != null) {
                    if (itemMap.get(data.getCheck_item_id()).getScore() == null) {
                        data.setPcheckname(itemMap.get(data.getCheck_item_id()).getName());
                    } else {
                        data.setPcheckname(itemMap.get(itemMap.get(data.getCheck_item_id()).getParent_id()).getName());
                    }
                }
            }
            if (codeMap.get(data.getWrecker_check_type()) != null) {
                data.setWrecker_check_type_name(codeMap.get(data.getWrecker_check_type()).getCode_name());
            }
            if (codeMap.get(data.getSpecial_check_type()) != null) {
                data.setSpecial_check_type_name(codeMap.get(data.getSpecial_check_type()).getCode_name());
            }
        }

        // 获得所有服务区数据
        List<FeeStation> saList = commonService.getAllDeptByLayer("SA");
        // 获得所有道管中心数据
        List<FeeStation> rmList = commonService.getAllDeptByLayer("RM");

        int checkType = fwqCheckOther.getCheck_type();
        if (checkType == 8 || checkType == 9) {
            Long id = 26L;
            if (checkType == 8) {
                id = 26L;
            }
            if (checkType == 9) {
                id = 27L;
            }
            //获取考核办法对应的考核项
            List<String> itemName = commonService.getCheckItemName(id);

            for (FwqCheckOther c : checkOtherList) {
                if (!itemName.contains(c.getPcheckname())) {
                    itemName.add(c.getPcheckname());
                }
            }

            for (FeeStation station : saList) {
                FwqCheckOtherStatistics fwqCheckOtherStatistics = new FwqCheckOtherStatistics();
                fwqCheckOtherStatistics.setDeptid(station.getDeptid());
                fwqCheckOtherStatistics.setDeptname(station.getName().replaceAll("服务区", ""));
                fwqCheckOtherStatistics.setPdeptid(station.getParentid());
                HashMap<String, Integer> s = new HashMap<>();
                for (String i : itemName) {
                    s.put(i, 0);
                }
                for (FwqCheckOther c : checkOtherList) {
                    if (c.getDept_id().toString().equals(station.getDeptid())) {
                        Integer i = s.get(c.getPcheckname());
                        if (i == null) {
                            i = 0;
                        }
//                        if (checkType == 2) {
                        s.put(c.getPcheckname(), Integer.valueOf((int) (i + c.getCheck_score())));
//                        } else {
//                            if (c.getOrgan_name() == null || "".equals(c.getOrgan_name())) {
//                                s.put(c.getPcheckname(), Integer.valueOf((int) (i + c.getCheck_score())));
//                            } else {
//                                s.put(c.getPcheckname(), i);
//                            }
//                        }
                    }
                }
                fwqCheckOtherStatistics.setScoremap(s);
                fwqCheckOtherStatisticses.add(fwqCheckOtherStatistics);
            }

            for (FeeStation a : rmList) {
                List<FwqCheckOtherStatistics> s = new ArrayList<>();
                for (FwqCheckOtherStatistics o : fwqCheckOtherStatisticses) {
                    if (o.getPdeptid().equals(a.getDeptid())) {
                        o.setPdeptname(a.getName());
                        s.add(o);
                    }
                }
                datas.put(a.getName().replaceAll("道管中心", ""), s);
            }

        }

        return datas;
    }



}
