package com.sifang.modules.supply.setting.service;


import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.setting.dao.CrmSupplyDeliveryMethodDao;
import com.sifang.modules.supply.setting.dao.CrmSupplyDeliveryMethodDetailDao;
import com.sifang.modules.supply.setting.entity.CrmSupplyDeliveryMethod;
import com.sifang.modules.supply.setting.entity.CrmSupplyDeliveryMethodDetail;
import com.sifang.modules.supply.setting.entity.CrmSupplyMaterialType;
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.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class CrmSupplyDeliveryMethodService extends BaseService {

    @Autowired
    CrmSupplyDeliveryMethodDao deliveryMethodDao;

    public LayuiPage deliveryMethodList(LayuiPage page, Map<String, String> map) {
        User user = UserUtils.getUser();
        List<Record> list = deliveryMethodDao.deliveryMethodList(page, map);
        page.setData(list);
        page.setCount(deliveryMethodDao.deliveryMethodListCount(map));
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveDeliveryMethod(String name, String id) {
        if (deliveryMethodDao.checkName(name, id)) {
            throw new BusinessException("发货方式已存在，请勿重复添加!");
        }
        Date date = new Date();
        User user = UserUtils.getUser();
        CrmSupplyDeliveryMethod deliveryMethod;
        if (StringUtil.isBlank(id)) {
            deliveryMethod = new CrmSupplyDeliveryMethod();
            deliveryMethod.setCreateTime(date);
            deliveryMethod.setCreateBy(user.getId());
        } else {
            deliveryMethod = deliveryMethodDao.get(id);
            deliveryMethod.setUpdateTime(date);
        }
        deliveryMethod.setName(name);
        deliveryMethodDao.save(deliveryMethod);
    }

    public void deleteDeliveryMethod(String ids) {
        Db.update("UPDATE `crm_supply_delivery_method` SET STATUS='2',update_time=NOW() WHERE name<>'快递物流' and  id in (" + StringUtil.joinInSql(ids.split(",")) + ")");
    }

    public CrmSupplyDeliveryMethod getTypeDetai(String id) {
        CrmSupplyDeliveryMethod deliveryMethod = deliveryMethodDao.get(id);
        return deliveryMethod;
    }

    /**
     * 获取发货方式列表
     */
    public List<Record> listDeliverMethods() {
        return Db.find("select * from crm_supply_delivery_method where status='1' order by create_time");
    }

    public Record findMaterislAll(Map<String,String> map){
        Record re = deliveryMethodDao.findMaterislAll(map);
        Long count  = deliveryMethodDao.findthd(map.get("traderId"));
        re.set("thddCount",count);
        return re;

    }



}
