package com.xjrsoft.module.evaluate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.module.evaluate.dto.AddEvaluateTemplateDto;
import com.xjrsoft.module.evaluate.dto.EvaluateTemplatePageDto;
import com.xjrsoft.module.evaluate.dto.UpdateEvaluateTemplateStatusDto;
import com.xjrsoft.module.evaluate.entity.EvaluateTemplate;
import com.xjrsoft.module.evaluate.mapper.EvaluateTemplateMapper;
import com.xjrsoft.module.evaluate.service.IEvaluateTemplateService;
import com.xjrsoft.module.evaluate.vo.EvaluateTemplatePageVo;
import com.xjrsoft.module.evaluate.vo.EvaluateTemplateVo;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.mapper.DepartmentMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
* @title: 评价模版
* @Author szs
* @Date: 2024-01-15
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class EvaluateTemplateServiceImpl extends MPJBaseServiceImpl<EvaluateTemplateMapper, EvaluateTemplate> implements IEvaluateTemplateService {
    private final EvaluateTemplateMapper evaluateTemplateMapper;
    private final DepartmentMapper departmentMapper;

    @Override
    public Page<EvaluateTemplatePageVo> getPage(Page<EvaluateTemplatePageDto> page, EvaluateTemplatePageDto dto) {
        Page<EvaluateTemplatePageVo> voPage = evaluateTemplateMapper.getPage(page, dto);
        List<Department> departments = departmentMapper.selectList(
                new QueryWrapper<Department>().lambda().eq(Department::getDeleteMark, DeleteMark.NODELETE.getCode())
        );
        Map<String, String> departmentMap = new HashMap<>();
        for (Department department : departments) {
            departmentMap.put(department.getId().toString(), department.getName());
        }

        for (EvaluateTemplatePageVo record : voPage.getRecords()) {
            String[] deptIds = record.getDeptIds().replaceAll("\\[", "").replaceAll("]", "").replaceAll(" ", "").split(",");
            StringBuilder deptIdSb = new StringBuilder();
            StringBuilder deptName = new StringBuilder();
            for (int i = 0; i < deptIds.length; i ++){
                if(i > 0){
                    deptName.append(",");
                    deptIdSb.append(",");
                }
                if (deptIds[i].equals("0")) {
                    deptName.append("通用");
                } else {
                    deptName.append(departmentMap.get(deptIds[i]));
                }
                deptIdSb.append(deptIds[i]);
            }
            record.setDeptNames(deptName.toString().equals("null")? "通用": deptName.toString());
            record.setDeptIds("[" + deptIdSb.toString() + "]");
        }
        return voPage;
    }

    @Override
    public EvaluateTemplateVo getInfo(Long id) {
        EvaluateTemplate evaluateTemplate = this.getById(id);
        EvaluateTemplateVo evaluateTemplateVo = BeanUtil.toBean(evaluateTemplate, EvaluateTemplateVo.class);
        List<Department> departments = departmentMapper.selectList(
                new QueryWrapper<Department>().lambda().eq(Department::getDeleteMark, DeleteMark.NODELETE.getCode())
        );
        Map<String, String> departmentMap = new HashMap<>();
        for (Department department : departments) {
            departmentMap.put(department.getId().toString(), department.getName());
        }

        String[] deptIds = evaluateTemplateVo.getDeptIds().replaceAll("\\[", "").replaceAll("]", "").replaceAll(" ", "").split(",");
        StringBuilder deptIdSb = new StringBuilder();
        StringBuilder deptName = new StringBuilder();
        for (int i = 0; i < deptIds.length; i ++){
            if(i > 0){
                deptName .append(",");
                deptIdSb.append(",");
            }
            deptName.append(departmentMap.get(deptIds[i]));
            deptIdSb.append(deptIds[i]);
        }

        evaluateTemplateVo.setDeptNames(deptName.equals("null")? "通用": deptName.toString());
        evaluateTemplateVo.setDeptIds("[" + deptIdSb.toString() + "]");
        return evaluateTemplateVo;
    }

    @Override
    public Boolean saveCustom(AddEvaluateTemplateDto dto) {
        EvaluateTemplate template = BeanUtil.toBean(dto, EvaluateTemplate.class);
        String[] split = dto.getDeptIds().split(",");
        JsonArray array = new JsonArray();
        for (String orgId : split) {
            JsonObject orgObj = new JsonObject();
            orgObj.addProperty("org_id", orgId.trim());
            array.add(orgObj);
        }
        template.setDeptIds(array.toString());
        evaluateTemplateMapper.insert(template);
        return true;

    }

    @Override
    public Boolean changeStatus(UpdateEvaluateTemplateStatusDto dto) {
        EvaluateTemplate evaluateTemplate = evaluateTemplateMapper.selectById(dto.getId());
        UpdateWrapper<EvaluateTemplate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", evaluateTemplate.getId());
        updateWrapper.setSql("status = "  + dto.getStatus());
        evaluateTemplateMapper.update(evaluateTemplate, updateWrapper);
        return true;
    }
}
