package com.sifang.modules.insurance.service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.insurance.constants.InsuranceConstants;
import com.sifang.modules.insurance.dao.InsuranceListDao;
import com.sifang.modules.insurance.entity.CrmInsuranceList;
import com.sifang.modules.insurance.request.InsuranceListEditReq;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * @author DQChen
 * @date 2024-01-18 9:52
 */
@Service
public class InsuranceListService extends BaseService {

    @Autowired
    InsuranceListDao insuranceListDao;

    /**
     * 保险公司列表
     * @param queryParams
     * @param page
     */
    public void list(Map<String, String> queryParams, LayuiPage page){
        page.setData(insuranceListDao.list(queryParams, page));
        page.setCount(insuranceListDao.countList(queryParams));
    }

    public CrmInsuranceList findById(String id) {
        return insuranceListDao.get(id);
    }

    public Record selectDetailById(String id) {
        return insuranceListDao.selectDetailById(id);
    }

    public Result addInsurance(InsuranceListEditReq addReq) {
        if(StringUtil.isAnyBlank(addReq.getName(), addReq.getType(), addReq.getCompanyId(), addReq.getCompanyName()
                , addReq.getPolicyholder(), addReq.getInsurant())){
            return Result.fail("缺少必要参数");
        }

        long count = insuranceListDao.queryCountSame(addReq.getPolicyholder(), addReq.getType(), addReq.getName(), null);
        if (count > 0) {
            return Result.fail("投保人相同的保险类型不可重复");
        }

        Date now = new Date();
        User user = UserUtils.getUser();
        CrmInsuranceList insuranceList = new CrmInsuranceList();
        insuranceList.setName(addReq.getName());
        insuranceList.setType(addReq.getType());
        insuranceList.setCompanyId(addReq.getCompanyId());
        insuranceList.setCompanyName(addReq.getCompanyName());
        insuranceList.setPolicyholder(addReq.getPolicyholder());
        insuranceList.setInsurant(addReq.getInsurant());
        insuranceList.setStatus(InsuranceConstants.STATUS_NORMAL);
        insuranceList.setCreateTime(now);
        insuranceList.setCreateBy(user.getId());
        insuranceList.setCreator(user.getUserName());
        insuranceListDao.save(insuranceList);
        return Result.ok();
    }

    public Result editInsurance(InsuranceListEditReq editReq) {
        if (StringUtil.isAnyBlank(editReq.getId(), editReq.getName(), editReq.getType(), editReq.getCompanyId(), editReq.getCompanyName()
                , editReq.getPolicyholder(), editReq.getInsurant())) {
            return Result.fail("缺少必要参数");
        }

        long count = insuranceListDao.queryCountSame(editReq.getPolicyholder(), editReq.getType(), editReq.getName(), editReq.getId());
        if (count >0 ) {
            return Result.fail("投保人相同的保险类型不可重复");
        }

        Date now = new Date();
        User user = UserUtils.getUser();
        CrmInsuranceList insuranceList = insuranceListDao.get(editReq.getId());
        insuranceList.setName(editReq.getName());
        insuranceList.setType(editReq.getType());
        insuranceList.setCompanyId(editReq.getCompanyId());
        insuranceList.setCompanyName(editReq.getCompanyName());
        insuranceList.setPolicyholder(editReq.getPolicyholder());
        insuranceList.setInsurant(editReq.getInsurant());
        insuranceList.setStatus(InsuranceConstants.STATUS_NORMAL);
        insuranceList.setUpdateTime(now);
        insuranceList.setUpdateBy(user.getId());
        insuranceList.setModifier(user.getUserName());
        insuranceListDao.save(insuranceList);
        return Result.ok();
    }


    public Result disable(String id) {
        CrmInsuranceList insuranceList = insuranceListDao.get(id);
        if (!InsuranceConstants.STATUS_NORMAL.equals(insuranceList.getStatus())) {
            return Result.fail("状态已更新 请刷新后再试");
        }
        User user = UserUtils.getUser();
        insuranceList.setStatus(InsuranceConstants.STATUS_FORBIDDEN);
        insuranceList.setUpdateBy(user.getId());
        insuranceList.setUpdateTime(new Date());
        insuranceList.setModifier(user.getUserName());
        insuranceListDao.save(insuranceList);
        return Result.ok();
    }

    public Result enable(String id) {
        CrmInsuranceList insuranceList = insuranceListDao.get(id);
        if (!InsuranceConstants.STATUS_FORBIDDEN.equals(insuranceList.getStatus())) {
            return Result.fail("状态已更新 请刷新后再试");
        }
        User user = UserUtils.getUser();
        insuranceList.setStatus(InsuranceConstants.STATUS_NORMAL);
        insuranceList.setUpdateBy(user.getId());
        insuranceList.setUpdateTime(new Date());
        insuranceList.setModifier(user.getUserName());
        insuranceListDao.save(insuranceList);
        return Result.ok();
    }

    public Result del(String id) {
        CrmInsuranceList insuranceList = insuranceListDao.get(id);
        if (!InsuranceConstants.STATUS_FORBIDDEN.equals(insuranceList.getStatus())) {
            return Result.fail("状态已更新 请刷新后再试");
        }
        User user = UserUtils.getUser();
        insuranceList.setStatus(InsuranceConstants.STATUS_DEL);
        insuranceList.setUpdateBy(user.getId());
        insuranceList.setUpdateTime(new Date());
        insuranceList.setModifier(user.getUserName());
        insuranceListDao.save(insuranceList);
        return Result.ok();
    }

    public List<Record> insuranceList() {
        return insuranceListDao.insuranceList();
    }

    // 修改
    public List<Record> insuranceLists(String id) {
        return insuranceListDao.insuranceLists(id);
    }

}
