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

import com.jfinal.kit.StrKit;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.order.constant.DeliverOrderConstants;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialApplyMsg;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialDeliverOrderMsg;
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 MaterialDeliverOrderMsgDao extends BaseDao<CrmSupplyMaterialDeliverOrderMsg> {

    public List<CrmSupplyMaterialDeliverOrderMsg> listMsgByOrderId(String id) {
        return find("from CrmSupplyMaterialDeliverOrderMsg where orderId=:p1 order by operateTime asc", new Parameter(id));
    }

    public void saveOptMsg(String orderId, Short type, String append, Operator operator) {
        CrmSupplyMaterialDeliverOrderMsg msg = new CrmSupplyMaterialDeliverOrderMsg();
        msg.setOrderId(orderId);
        msg.setType(type);
        User user = UserUtils.getUser();
        String newName = UserUtils.findName(user);
        msg.setOperator(newName);
        msg.setOperatorId(operator.getUserId());
        msg.setOperateTime(new Date());
        String content = "";
        switch (type) {
            case DeliverOrderConstants.OPT_DELETE:
                content += "删除订单";
                break;
            case DeliverOrderConstants.OPT_CREATE:
                content += "创建订单";
                break;
            case DeliverOrderConstants.OPT_EDIT:
                content += "编辑/暂存订单";
                break;
            case DeliverOrderConstants.OPT_CANCEL:
                content += "撤销订单";
                break;
            case DeliverOrderConstants.OPT_CHANGE:
                content += "改派订单";
                break;
            case DeliverOrderConstants.OPT_SPLIT:
                content += "拆单";
                break;
            case DeliverOrderConstants.OPT_FH:
                content += "发货";
                break;
            case DeliverOrderConstants.OPT_DH:
                content += "发货打回";
                break;
            case DeliverOrderConstants.OPT_RK:
                content += "签收";
                break;
            case DeliverOrderConstants.OPT_AUDIT_FAIL:
                content += "驳回";
                break;
            case DeliverOrderConstants.OPT_MERGE:
                content += "合并订单";
                break;
            case DeliverOrderConstants.FHD_SH_TG:
                content += "发货单审核通过";
                break;
            case DeliverOrderConstants.FHD_SH_NOT:
                content += "发货单审核驳回";
                break;
            default:
                throw new IllegalArgumentException("unknown type");
        }
        if (StrKit.notBlank(append)) {
            content += "," + append;
        }
        msg.setContent(content);
        save(msg);
    }

    public void saveOrderOpe(String orderId, String content,String userName,String userId,Short type){
        CrmSupplyMaterialDeliverOrderMsg msg = new CrmSupplyMaterialDeliverOrderMsg();
        msg.setOrderId(orderId);
        msg.setType(type);
        User user = UserUtils.getUser();
        String newName = UserUtils.findName(user);
        msg.setOperator(newName);
        msg.setOperatorId(userId);
        msg.setOperateTime(new Date());
        if(StringUtil.isBlank(content)){
            content="";
        }
        msg.setContent(content);
        save(msg);
    }

}
