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

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerFieldConfigMapper;
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.CkCleanerFieldConfigQuery;
import cn.com.countrygarden.bi.main.dto.ck.CkCleanerFieldConfigVo;
import cn.com.countrygarden.bi.main.dto.ck.CkCleanerVo;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.ck.CkCleanerFieldConfig;
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.CkCleanerExpandService;
import cn.com.countrygarden.bi.main.service.ck.CkCleanerFieldConfigService;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
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;

/**
 * @author 木木子
 * @Description: 字段配置表-业务逻辑层实现
 * @email 944528607@qq.com
 * @date 2020/4/27  15:25
 */
@Service
public class CkCleanerFieldConfigServiceImpl implements CkCleanerFieldConfigService {
    @Autowired
    private CkCleanerFieldConfigMapper ckCleanerFieldConfigMapper;

    @Autowired
    private CkCleanerExpandService ckCleanerService;

    @Autowired
    private CkCleanerRecordingMapper ckCleanerRecordingMapper;

    @Autowired
    private TbSysUserMapper tbSysUserMapper;

    @Override
    public Result queryPageList(CkCleanerFieldConfigQuery query) {
        Page<Object> pageList = PageHelper.startPage(query.getPage(), query.getSize());
        CkCleanerVo vo = new CkCleanerVo();
        if (StringUtil.isNotBlank(query.getFieldCode())) {
            vo.setFieldCode(query.getFieldCode());
        }
        if (StringUtil.isNotBlank(query.getFieldName())) {
            vo.setFieldCode(query.getFieldName());
        }
        List<CkCleanerFieldConfigVo> list = getAllCatalogue(vo, null);
        PageData data = new PageData(pageList);
        data.setData(pageList.getResult());
        return Response.response(data);
    }
    public List<CkCleanerFieldConfigVo> getAllCatalogue(CkCleanerVo vo, List<CkCleanerFieldConfigVo> list) {

        if (CollectionUtils.isEmpty(list)) {
            CkCleanerVo  ckCleanerVo = new CkCleanerVo();

            ckCleanerVo.setParentId(0);
            if (vo.getId()!=null) {
                ckCleanerVo.setId(vo.getId());
            }
//            if (StringUtil.isNotBlank(vo.getFieldCode())) {
//                ckCleanerVo.setFieldCode(vo.getFieldCode());
//            }
//            if (StringUtil.isNotBlank(vo.getFieldName())) {
//                ckCleanerVo.setFieldCode(vo.getFieldName());
//            }
            list = ckCleanerFieldConfigMapper.selectListData(ckCleanerVo);
        }
        for (CkCleanerFieldConfigVo config : list) {
            CkCleanerVo    ckCleanerVo = new CkCleanerVo();


            ckCleanerVo.setParentId(config.getId());
            if (vo.getId()!=null) {
                ckCleanerVo.setId(vo.getId());
            }
            if (vo.getType()!=null) {
                ckCleanerVo.setType(vo.getType());
            }
//            if (StringUtil.isNotBlank(vo.getFieldCode())) {
//                ckCleanerVo.setFieldCode(vo.getFieldCode());
//            }
//            if (StringUtil.isNotBlank(vo.getFieldName())) {
//                ckCleanerVo.setFieldCode(vo.getFieldName());
//            }
            List<CkCleanerFieldConfigVo> childList = ckCleanerFieldConfigMapper.selectListData(ckCleanerVo);
            if (CollectionUtils.isNotEmpty(childList)) {
                config.setChildren(childList);
                config.setChildrenCount(childList.size());
                config.setHasChildren(childList.size() > 0);
                ckCleanerVo.setParentId(null);
                getAllCatalogue(ckCleanerVo, childList);
            } else {
                continue;
            }
        }
        return list;
    }
//    public List<CkCleanerFieldConfigVo> getAllCatalogue(CkCleanerVo ckCleanerVo, List<CkCleanerFieldConfigVo> list) {
//
//        if (CollectionUtils.isEmpty(list)) {
//            ckCleanerVo = new CkCleanerVo();
//            ckCleanerVo.setId(ckCleanerVo.getId());
//            ckCleanerVo.setParentId(0);
//            list = ckCleanerFieldConfigMapper.selectListData(ckCleanerVo);
//        }
//        for (CkCleanerFieldConfigVo config : list) {
//            ckCleanerVo = new CkCleanerVo();
//            ckCleanerVo.setId(ckCleanerVo.getId());
//            ckCleanerVo.setType(ckCleanerVo.getType());
//            ckCleanerVo.setParentId(config.getId());
//            List<CkCleanerFieldConfigVo> childList = ckCleanerFieldConfigMapper.selectListData(ckCleanerVo);
//            if (CollectionUtils.isNotEmpty(childList)) {
//                config.setChildren(childList);
//                config.setChildrenCount(childList.size());
//                config.setHasChildren(childList.size() > 0);
//                ckCleanerVo.setParentId(null);
//                getAllCatalogue(ckCleanerVo, childList);
//            } else {
//                continue;
//            }
//        }
//        return list;
//    }

    @Override
    public Result queryById(Integer id) {
        return Response.response(ckCleanerFieldConfigMapper.selectByPrimaryKey(id));
    }

    @Override
    public Result addOrEdit(CkCleanerFieldConfig ckCleanerFieldConfig, AccessToken accessToken) {

        CkCleanerRecording recording=new CkCleanerRecording();
        recording.setStatus(1);
        Condition condition = new Condition(CkCleanerFieldConfig.class);
        condition.selectProperties("id,fieldCode,fieldName");
        Criteria criteria = condition.and();
        if (ckCleanerFieldConfig.getId() != null) {
            criteria.andNotEqualTo("id", ckCleanerFieldConfig.getId());
        }
        criteria.andEqualTo("status", 1).andEqualTo("fieldCode", ckCleanerFieldConfig.getFieldCode());
        List<CkCleanerFieldConfig> ckCleanerFieldConfigs = ckCleanerFieldConfigMapper.selectByCondition(condition);
        if (CollectionUtils.isNotEmpty(ckCleanerFieldConfigs)) {
            return Response.responseFail("编码：" + ckCleanerFieldConfig.getFieldCode() + "重复;");
        }

        ckCleanerFieldConfig.setStatus(ckCleanerFieldConfig.getStatus() == null ? 1 : ckCleanerFieldConfig.getStatus());
        ckCleanerFieldConfig.setParentId(ckCleanerFieldConfig.getParentId() == null ? 0 : ckCleanerFieldConfig.getParentId());
        ckCleanerFieldConfig.setStatus(1);
        ckCleanerFieldConfig.setUpdateBy(accessToken.getUserCode());
        ckCleanerFieldConfig.setUpdateTime(new Date());
        if (ckCleanerFieldConfig.getId() == null) {
            ckCleanerFieldConfig.setCreateBy(accessToken.getUserCode());
            ckCleanerFieldConfig.setCreateTime(new Date());
            ckCleanerFieldConfigMapper.insertSelective(ckCleanerFieldConfig);
            if (ckCleanerFieldConfig.getParentId() == 0) {
                return Response.response();
            }
            ckCleanerFieldConfig.setPath(ckCleanerFieldConfig.getParentId() + "/" + ckCleanerFieldConfig.getId());
            recording.setOperatingType("新增");
        }
        recording.setOperatingType("修改");
        //新增后拿到id需要处理path的值，所以新增成功后要再更新一次
        ckCleanerFieldConfigMapper.updateByPrimaryKey(ckCleanerFieldConfig);
        TbSysUser tbSysUser=tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());
        recording.setDataClassification("字段配置");
        recording.setOperatingName(tbSysUser.getUserName());
        recording.setOperatingBy(accessToken.getUserCode());
        recording.setOperatingTime(new Date());
        recording.setCreateTime(new Date());
        ckCleanerRecordingMapper.insert(recording);
        return Response.response();
    }


    @Override
    public Result del(Integer id, AccessToken accessToken) {
        CkCleanerRecording recording=new CkCleanerRecording();
        recording.setStatus(1);
        CkCleanerFieldConfig config = new CkCleanerFieldConfig();
        config.setId(id);
        config.setStatus(2);
        config.setUpdateTime(new Date());
        config.setUpdateBy(accessToken.getUserCode());
        int count = ckCleanerFieldConfigMapper.updateByPrimaryKeySelective(config);
        TbSysUser tbSysUser=tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());
        recording.setOperatingType("删除");
        recording.setDataClassification("字段配置");
        recording.setOperatingName(tbSysUser.getUserName());
        recording.setOperatingBy(accessToken.getUserCode());
        recording.setOperatingTime(new Date());
        recording.setCreateTime(new Date());
        ckCleanerRecordingMapper.insert(recording);
        return Response.response(count);
    }

    @Override
    public List<CkCleanerFieldConfigVo> optionList(CkCleanerVo query) {

        return ckCleanerService.formulaConfig(query, null);
    }

    @Override
    public Result statusEdit(Integer id, Integer status, AccessToken accessToken) {
        CkCleanerFieldConfig config = new CkCleanerFieldConfig();
        config.setStatus(status);
        config.setId(id);
        config.setUpdateBy(accessToken.getUserCode());
        config.setUpdateTime(new Date());
        return Response.response(ckCleanerFieldConfigMapper.updateByPrimaryKeySelective(config));

    }
}