package com.sifang.modules.supply.order.dao;

import com.jfinal.kit.StrKit;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialApplyDetail;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialApplyMsg;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.entity.Operator;
import org.springframework.stereotype.Repository;

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

/**
 * @author hongzq
 */
@Repository
public class MaterialApplyMsgDao extends BaseDao<CrmSupplyMaterialApplyMsg> {

    public List<CrmSupplyMaterialApplyMsg> listMsgByApplyId(String id) {
        return find("from CrmSupplyMaterialApplyMsg where applyId=:p1 order by operateTime desc", new Parameter(id));
    }

    public void saveOptMsg(String applyId, Short type, String append, Operator operator) {
        CrmSupplyMaterialApplyMsg msg = new CrmSupplyMaterialApplyMsg();
        msg.setApplyId(applyId);
        msg.setType(type);
        msg.setOperator(operator.getUserName());
        msg.setOperatorId(operator.getUserId());
        msg.setOperateTime(new Date());
        String content = msg.getOperator();
        switch (type) {
            case 0:
                content += "删除申请单";
                break;
            case 1:
                content += "创建申请单";
                break;
            case 2:
                content += "编辑申请单";
                break;
            case 3:
                content += "审批通过";
                break;
            case 4:
                content += "审批不通过";
                break;
            case 5:
                content += "操作下单";
                break;
            case 6:
                content += "操作拆单";
                break;
            case 7:
                content += "操作并单";
                break;
            default:
                throw new IllegalArgumentException("unknown type");
        }
        if (StrKit.notBlank(append)) {
            content += " " + append;
        }
        msg.setContent(content);
        save(msg);
    }

}
