package cn.com.countrygarden.bi.main.service.ck.impl;

import cn.com.countrygarden.bi.main.dao.ck.CkCleanerFieldConfigMapper;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerFileidRoleMapper;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerRecordingMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbSysUserMapper;
import cn.com.countrygarden.bi.main.dto.ck.CkCleanerVo;
import cn.com.countrygarden.bi.main.dto.ck.CkFileidRoleListVo;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.ck.CkCleanerFileidRole;
import cn.com.countrygarden.bi.main.model.ck.CkCleanerRecording;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser;
import cn.com.countrygarden.bi.main.service.ck.CkCleanerFileidRoleService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

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

/**
 * @Description:    字段配置角色表-业务逻辑层
 * @author  木木子
 * @email 944528607@qq.com
 * @date  2020/5/13  14:22
 */
@Service
public class CkCleanerFileidRoleServiceImpl implements CkCleanerFileidRoleService {
    @Autowired
    private CkCleanerFileidRoleMapper ckCleanerFileidRoleMapper;
    @Autowired
    private CkCleanerFieldConfigMapper ckCleanerFieldConfigMapper;
    @Autowired
    private TbSysUserMapper tbSysUserMapper;
    @Autowired
    private CkCleanerRecordingMapper ckCleanerRecordingMapper;
//    @Override
//    public Result queryPageList(CkCleanerFileidRole ckCleanerFileidRole, int page, int size) {
//        Page<Object> pageList = PageHelper.startPage(page, size);
//        List<CkFileidRoleListVo> list = getAllFileidRole(ckCleanerFileidRole.getRoleCode(), new CkCleanerVo(), null);
//        PageData data = new PageData(pageList);
//        data.setData(pageList.getResult());
//        return Response.response(data);
//    }
//
//    public List<CkFileidRoleListVo> getAllFileidRole(String roleCode, CkCleanerVo ckCleanerVo, List<CkFileidRoleListVo> list) {
//
//        if (CollectionUtils.isEmpty(list)) {
//            ckCleanerVo = new CkCleanerVo();
//            ckCleanerVo.setRoleCode(roleCode);
//            ckCleanerVo.setParentId(0);
//            list = ckCleanerFileidRoleMapper.getAllFileidRole(ckCleanerVo);
//        }
//        for (CkFileidRoleListVo config : list) {
//            ckCleanerVo = new CkCleanerVo();
//            ckCleanerVo.setRoleCode(roleCode);
//            ckCleanerVo.setParentId(config.getId());
//            List<CkFileidRoleListVo> childList = ckCleanerFileidRoleMapper.getAllFileidRole(ckCleanerVo);
//            if (CollectionUtils.isNotEmpty(childList)) {
//                config.setChildren(childList);
//                ckCleanerVo.setParentId(null);
//                getAllFileidRole(roleCode, ckCleanerVo, childList);
//            } else {
//                continue;
//            }
//        }
//        return list;
//    }




    @Override
    public int addOrEdit(String roleCode, String data, AccessToken accessToken) {
        List<CkCleanerFileidRole> fieldRoleList = JSON.parseArray(data, CkCleanerFileidRole.class);
        if(CollectionUtils.isNotEmpty(fieldRoleList)){
            for (CkCleanerFileidRole fileidRole : fieldRoleList) {
                fileidRole.setFieldCode(fileidRole.getFieldCode().split(",")[0]);
                fileidRole.setStatus(1);
                fileidRole.setUpdateBy(accessToken.getUserCode());
                fileidRole.setUpdateTime(new Date());
            }
        }
        Condition condition = new Condition(CkCleanerFileidRole.class);
        Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("roleCode", roleCode);
        int count = ckCleanerFileidRoleMapper.deleteByCondition(condition);
        if(CollectionUtils.isNotEmpty(fieldRoleList)){
            count += ckCleanerFileidRoleMapper.insertList(fieldRoleList);
        }
        TbSysUser tbSysUser = tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());

        String dataClassification="";
        if(roleCode.equals("ck_data_admin")){
            dataClassification="保洁数据上报字段配置";
        }else if(roleCode.equals("ck_afforest_admin")){
            dataClassification="绿化数据字段配置";
        }else if(roleCode.equals("ck_ksh_admin")){
            dataClassification="可视化数据字段配置";
        }

        CkCleanerRecording recording = new CkCleanerRecording();
        recording.setDataClassification(dataClassification);
        recording.setOperatingType("修改");
        recording.setCreateTime(new Date());
        recording.setOperatingTime(new Date());
        recording.setOperatingBy(accessToken.getUserCode());
        recording.setOperatingName(tbSysUser.getUserName());
        recording.setStatus(1);
        ckCleanerRecordingMapper.insert(recording);
        return count;
    }
@Override
    public List<CkCleanerFileidRole> checkedKeys(String roleCode, AccessToken accessToken) {
        Condition condition = new Condition(CkCleanerFileidRole.class);
        Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("roleCode", roleCode).andEqualTo("status",1);
        List<CkCleanerFileidRole> fileidRoleList = ckCleanerFileidRoleMapper.selectByCondition(condition);
        return fileidRoleList;
    }

    @Override
    public List<CkFileidRoleListVo> tree(CkCleanerFileidRole ckCleanerFileidRole) {
        List<CkFileidRoleListVo> list= buildTreeData( new CkCleanerVo(), null);
        return list;
    }

    @Override
    public List<CkFileidRoleListVo> cktree(CkCleanerFileidRole ckCleanerFileidRole,String role) {
        List<CkFileidRoleListVo> list= buildTreeCktreeData( new CkCleanerVo(), null,role);
        return list;
    }

    public List<CkFileidRoleListVo> buildTreeCktreeData(CkCleanerVo ckCleanerVo, List<CkFileidRoleListVo> list,String role) {

        if (CollectionUtils.isEmpty(list)) {
            ckCleanerVo=new CkCleanerVo();
            ckCleanerVo.setId(ckCleanerVo.getId());
            ckCleanerVo.setParentId(0);
            list = ckCleanerFieldConfigMapper.selectFieldConfigTree(ckCleanerVo);
        }
        for (CkFileidRoleListVo config : list) {
            ckCleanerVo=new CkCleanerVo();
            ckCleanerVo.setId(ckCleanerVo.getId());
            ckCleanerVo.setType(ckCleanerVo.getType());
            ckCleanerVo.setParentId(config.getId());
            ckCleanerVo.setFieldCode(config.getFieldCode());
            List<CkFileidRoleListVo> childList = ckCleanerFieldConfigMapper.selectFieldConfigTree(ckCleanerVo);
            if (CollectionUtils.isNotEmpty(childList)) {
                config.setChildren(childList);
                ckCleanerVo.setParentId(null);
                buildTreeCktreeData(ckCleanerVo, childList, role);
            } else {
                continue;
            }
        }

        return list;
    }

    /**
     * 构建字段配置列表的树结构数据
     * @param ckCleanerVo
     * @param list
     * @return
     */
    public List<CkFileidRoleListVo> buildTreeData(CkCleanerVo ckCleanerVo, List<CkFileidRoleListVo> list) {

        if (CollectionUtils.isEmpty(list)) {
            ckCleanerVo=new CkCleanerVo();
            ckCleanerVo.setId(ckCleanerVo.getId());
            ckCleanerVo.setParentId(0);
            list = ckCleanerFieldConfigMapper.selectFieldConfigTree(ckCleanerVo);
        }
        for (CkFileidRoleListVo config : list) {
            ckCleanerVo=new CkCleanerVo();
            ckCleanerVo.setId(ckCleanerVo.getId());
            ckCleanerVo.setType(ckCleanerVo.getType());
            ckCleanerVo.setParentId(config.getId());
            List<CkFileidRoleListVo> childList = ckCleanerFieldConfigMapper.selectFieldConfigTree(ckCleanerVo);
            if (CollectionUtils.isNotEmpty(childList)) {
                config.setChildren(childList);
                ckCleanerVo.setParentId(null);
                buildTreeData(ckCleanerVo, childList);
            } else {
                continue;
            }
        }
        return list;
    }
}