package com.sifang.modules.customer.Service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.dao.CrmReasonForApplicationDao;
import com.sifang.modules.customer.entity.CrmReasonForApplication;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
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 java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CrmReasonForApplicationService extends BaseService {


    @Autowired
    private CrmReasonForApplicationDao applicationDaoDao;

    public LayuiPage materialTypeList(LayuiPage page, Map<String, String> map) {
        List<Record> list = applicationDaoDao.getApplicationList(page, map);
        page.setData(list);
        page.setCount(applicationDaoDao.reasonForApplicationCount(map));
        return page;
    }

    public void saveMaterialType(String name, String id) {
        if (applicationDaoDao.checkName(name, id)) {
            throw new BusinessException("物料类型已存在，请勿重复添加!");
        }
        Date date = new Date();
        User user = UserUtils.getUser();
        CrmReasonForApplication materialType;
        if (StringUtil.isBlank(id)) {
            materialType = new CrmReasonForApplication();
            materialType.setCreateTime(date);
            materialType.setCreateBy(user.getId());
        } else {
            materialType = applicationDaoDao.get(id);
            materialType.setUpdateTime(date);
        }
        materialType.setName(name);
        materialType.setType("2");
        applicationDaoDao.save(materialType);
    }

    public void deleteMaterialType(String ids) {
        Db.update("UPDATE `crm_reason_for_application` SET status='2',update_time=NOW() WHERE id in (" + StringUtil.joinInSql(ids.split(",")) + ")");
    }

    public CrmReasonForApplication getTypeDetai(String id) {
        CrmReasonForApplication materialType = applicationDaoDao.get(id);
        return materialType;
    }

    public List<Record> getApplicationList(){
        List<Record> list = applicationDaoDao.listreasonForApplication();
        return list;
    }

}
