package com.sifang.modules.sunshine.SystemSetting.service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.dao.ProductLineSetDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmRectificationSet;
import com.sifang.modules.sunshine.SystemSetting.entity.ProductLineSet;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class ProductLineSetService extends BaseService {

    @Autowired
    private ProductLineSetDao dao;

    /**
     * 获取设置列表
     */
    public List<Record> getList(Map<String, String> params) {
        return dao.findList(params);
    }

    /**
     * 查询变更类型设置详情
     */
    public Record getOneById(String id) {
        return dao.findOneById(id);
    }

    /**
     * 查询业务属性
     */
    public String getProductLineAttribute(String id) {
        Record re = getOneById(id);
        if (re == null) {
            return "";
        }
        switch (re.getStr("attribute")) {
            case "1":
                return "BT业务";
            case "2":
                return "经营性租赁";
            case "3":
                return "融资租赁";
            case "4":
                return "光伏贷";
            case "5":
                return "全款";
            default:
                return "其他";
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Result add(String name, String attribute,  Result result) {
        // 校验名称
        long total = dao.queryCountByName(name, null);
        if (total > 0) {
            result.setCode("206");
            result.setMsg("已存在相同类型名称，请重新设置");
            return result;
        }
        User user = UserUtils.getUser();
        ProductLineSet set = new ProductLineSet();
        set.setName(name);
        set.setAttribute(attribute);
        set.setCreateBy(user.getId());
        set.setCreateTime(new Date());
        dao.save(set);
        result.setCode("200");
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public Result edit(String id, String name, String attribute,  Result result) {
        // 校验名称
        long total = dao.queryCountByName(name, id);
        if (total > 0) {
            result.setCode("206");
            result.setMsg("已存在相同类型名称，请重新设置");
            return result;
        }
        ProductLineSet set = dao.get(id);
        set.setName(name);
        set.setAttribute(attribute);
        set.setUpdateTime(new Date());
        dao.save(set);
        result.setCode("200");
        return result;
    }

    public void deleteById(String id) {
        dao.deleteById(id);
    }
}
