package cn.palmte.work.service;

import cn.palmte.work.model.ProcurementType;
import cn.palmte.work.model.ProcurementTypeRepository;
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;

@Service
public class ProcurementTypeService {

    @Autowired
    private ProcurementTypeRepository procurementTypeRepository;

    @Autowired
    private Pagination pagination;

    public Page<ProcurementType> list(Map<String, String> searchInfo, int pageNumber, int pageSize){
        QueryHelper queryHelper = new QueryHelper("SELECT *","procurement_type");
        queryHelper.addCondition("is_deleted=0");
        queryHelper.addCondition(searchInfo.containsKey("name"), "name like ?", "%" +
                searchInfo.get("name") + "%");
        queryHelper.addCondition(searchInfo.containsKey("type") && !"-1".equals(searchInfo.get("type")),
                "type=" + searchInfo.get("type"));
        queryHelper.addCondition(searchInfo.containsKey("enabled") && !"-1".equals(searchInfo.get("enabled")),
                "enabled=" + searchInfo.get("enabled"));
        queryHelper.addCondition(searchInfo.containsKey("startTime"), "created_time >= ?", searchInfo.get("startTime") + " 00:00:00");
        queryHelper.addCondition(searchInfo.containsKey("endTime"), "created_time <= ?", searchInfo.get("endTime") + " 23:59:59");
        queryHelper.addOrderProperty("created_time", false);
        Page<ProcurementType> paginate = pagination.paginate(queryHelper.getSql(), ProcurementType.class, pageNumber, pageSize);
        List<ProcurementType> list = paginate.getList();
        typeName(list);
        paginate.setList(list);
        return paginate;
    }

    public void typeName(List<ProcurementType> list){
        if(CollectionUtil.isNotEmpty(list)){
            for (ProcurementType procurementType : list) {
                if(procurementType.getType() == 1){
                    procurementType.setTypeName("设备");
                }
                if(procurementType.getType() == 2){
                    procurementType.setTypeName("服务");
                }
                if(procurementType.getType() == 3){
                    procurementType.setTypeName("施工");
                }
                if(procurementType.getType() == 4){
                    procurementType.setTypeName("其他");
                }
            }
        }
    }

    public ProcurementType findOne(int id) {
        return procurementTypeRepository.findOne(id);
    }

    public void saveOtUpdate(int procurementTypeId, ProcurementType procurementType) {
        ProcurementType obj = procurementTypeRepository.findOne(procurementTypeId);
        if(null == obj){
            obj = new ProcurementType();
            obj.setName(procurementType.getName());
            obj.setType(procurementType.getType());
            obj.setEnabled(procurementType.getEnabled());
            obj.setCreatedBy(InterfaceUtil.getAdmin().getRealName());
            obj.setCreatedTime(new Date());
        }else {
            obj.setName(procurementType.getName());
            obj.setType(procurementType.getType());
            obj.setEnabled(procurementType.getEnabled());
        }
        procurementTypeRepository.saveAndFlush(obj);
    }

    public boolean deleteByIDs(String[] deleteIds) {
        boolean deleted = true;
        try {
            for (String id : deleteIds) {
                ProcurementType procurementType = procurementTypeRepository.findOne(Integer.parseInt(id));
                if(null != procurementType){
                    procurementType.setIsDeleted(1);
                    procurementTypeRepository.saveAndFlush(procurementType);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            deleted = false;
        }

        return deleted;
    }

    public boolean enableOrDisable(int status, int id) {
        ProcurementType one = procurementTypeRepository.findOne(id);
        one.setEnabled(status);
        ProcurementType procurementType = procurementTypeRepository.saveAndFlush(one);

        return null != procurementType;
    }

    public List<ProcurementType> allProcurementTypeList(){
        return procurementTypeRepository.findAllByEnabledEqualsAndIsDeletedEquals(ProcurementType.ENABLED,ProcurementType.NOTDELETE);
    }
}
