package cn.palmte.work.service;

import cn.palmte.work.model.Admin;
import cn.palmte.work.model.Dept;
import cn.palmte.work.model.DeptRepository;
import cn.palmte.work.model.ProcurementType;
import cn.palmte.work.utils.InterfaceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.jfunc.common.db.QueryHelper;
import top.jfunc.common.db.bean.Page;
import top.jfunc.common.db.utils.Pagination;
import top.jfunc.common.utils.CollectionUtil;

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

/**
 * @author xiongshiyan at 2021/10/29 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@Service
public class DeptService {
    @Autowired
    private DeptRepository deptRepository;

    @Autowired
    private Pagination pagination;

    public List<Dept> findAll(){
        return deptRepository.findAll();
    }

    public Dept findUserDept(Admin admin){
        return deptRepository.findOne(admin.getDeptId());
    }

    public Page<Dept> list(Map<String, String> searchInfo, int pageNumber, int pageSize){
        QueryHelper queryHelper = new QueryHelper("SELECT d.*, (CASE d.enabled WHEN 0 THEN '已禁用' WHEN 1 THEN '已启用' END) as status, su.real_name as realName, (CASE WHEN d.level = 1 THEN '' ELSE p.name END) as parentName","dept d");
        queryHelper.leftJoin("dept p", "d.parent_id = p.id");
        queryHelper.leftJoin("sys_user su", "d.manager_id = su.id");
        queryHelper.addCondition(searchInfo.containsKey("name"), "d.name like ?", "%" +
                searchInfo.get("name") + "%");
        queryHelper.addCondition(searchInfo.containsKey("enabled") && !"-1".equals(searchInfo.get("enabled")),
                "d.enabled=" + searchInfo.get("enabled"));
        queryHelper.addCondition(searchInfo.containsKey("parentId") && !"-1".equals(searchInfo.get("parentId")),
                "d.parent_id=" + searchInfo.get("parentId"));
        queryHelper.addCondition(searchInfo.containsKey("startTime"), "d.created_time >= ?", searchInfo.get("startTime") + " 00:00:00");
        queryHelper.addCondition(searchInfo.containsKey("endTime"), "d.created_time <= ?", searchInfo.get("endTime") + " 23:59:59");
        queryHelper.addOrderProperty("d.id", false);
        Page<Dept> paginate = pagination.paginate(queryHelper.getSql(), Dept.class, pageNumber, pageSize);
        return paginate;
    }

    public Dept findOne(int id) {
        return deptRepository.findOne(id);
    }

    public void saveOtUpdate(int deptId, Dept dept) {
        Dept obj = deptRepository.findOne(deptId);
        if(null == obj){
            obj = new Dept();
            obj.setName(dept.getName());
            obj.setManagerId(dept.getManagerId());
            obj.setParentId(dept.getParentId());
            if (obj.getParentId() == 0) {
                obj.setLevel(1);
            } else {
                Dept one = deptRepository.findOne(obj.getParentId());
                obj.setLevel(one.getLevel() + 1);
            }
            obj.setEnabled(dept.getEnabled());
            obj.setCreatedBy(InterfaceUtil.getAdmin().getRealName());
            obj.setCreatedTime(new Date());
        }else {
            obj.setName(dept.getName());
            obj.setManagerId(dept.getManagerId());
            obj.setParentId(dept.getParentId());
            if (obj.getParentId() == 0) {
                obj.setLevel(1);
            } else {
                Dept one = deptRepository.findOne(obj.getParentId());
                obj.setLevel(one.getLevel() + 1);
            }
            obj.setEnabled(dept.getEnabled());
        }
        deptRepository.saveAndFlush(obj);
    }

    public boolean enableOrDisable(int status, int id) {
        Dept one = deptRepository.findOne(id);
        one.setEnabled(status);
        Dept dept = deptRepository.saveAndFlush(one);

        return null != dept;
    }
}
