package com.mortals.xhx.module.matter.web;

import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.key.ParamKey;
import com.mortals.xhx.module.sample.model.SampleBillEntity;
import com.mortals.xhx.module.sample.service.SampleBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.matter.model.MatterDatumEntity;
import com.mortals.xhx.module.matter.service.MatterDatumService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

/**
 * 事项申请材料
 *
 * @author zxfei
 * @date 2022-09-27
 */
@RestController
@RequestMapping("matter/datum")
public class MatterDatumController extends BaseCRUDJsonBodyMappingController<MatterDatumService, MatterDatumEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private SampleBillService sampleBillService;

    public MatterDatumController() {
        super.setModuleDesc("事项申请材料");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "source", paramService.getParamBySecondOrganize("MatterDatum", "source"));
        this.addDict(model, "isRecommend", paramService.getParamBySecondOrganize("MatterDatum", "isRecommend"));
        super.init(model, context);
    }

    @Override
    protected void doListBefore(MatterDatumEntity query, Map<String, Object> model, Context context) throws AppException {
        List<OrderCol> orderColList = new ArrayList<>();
        orderColList.add(new OrderCol("isRecommend", OrderCol.DESCENDING));
        orderColList.add(new OrderCol("createTime", OrderCol.DESCENDING));
        query.setOrderColList(orderColList);
        super.doListBefore(query, model, context);
    }

/*    @Override
    protected int doListAfter(MatterDatumEntity query, Map<String, Object> model, Context context) throws AppException {
        int recommendCount = paramService.getParamIntValue(ParamKey.MATTER_DATUM_RECOMMEND_COUNT);
        model.put("recommendCount", recommendCount);
        return 1;
    }*/

    @RequestMapping(value = {"addPubdatum"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String addPubdatum(Long[] ids, Long matterId, Long siteId) {
        Map<String, Object> model = new HashMap();
        int code = VALUE_RESULT_SUCCESS;
        String busiDesc = "添加公共库";
        try {
            Rest<String> rest = this.service.addPubdatum(ids, matterId, siteId, getContext());
            model.put(MESSAGE_INFO, rest.getMsg());
            this.recordSysLog(this.request, rest.getMsg());
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(this.request, busiDesc, model, e);
        }

        JSONObject ret = new JSONObject();
        ret.put("code", Integer.valueOf(code));
        ret.put("msg", model.remove(MESSAGE_INFO));
        ret.put("data", model);
        return ret.toJSONString();
    }



    @RequestMapping(value = {"recommend"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String recommend(Long id, Long siteId) {
        Map<String, Object> model = new HashMap();
        int code = VALUE_RESULT_SUCCESS;
        String busiDesc = "推荐or取消推荐" + this.getModuleDesc();
        try {
            Rest<String> rest = this.service.recommend(id, siteId, getContext());
            model.put("message_info", rest.getMsg());
            this.recordSysLog(this.request, rest.getMsg());
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(this.request, busiDesc, model, e);
        }

        JSONObject ret = new JSONObject();
        ret.put("code", code);
        ret.put("msg", model.remove("message_info"));
        ret.put("data", model);
        return ret.toJSONString();
    }


    @Override
    protected int infoAfter(Long id, Map<String, Object> model, MatterDatumEntity entity, Context context) throws AppException {
        SampleBillEntity sampleBillEntity = new SampleBillEntity();
        sampleBillEntity.initAttrValue();
        sampleBillEntity.setSiteId(entity.getSiteId());
        sampleBillEntity.setMaterialName(entity.getMaterialName());
        sampleBillEntity.setMaterialFullName(entity.getMateriaFullName());
        sampleBillEntity.setMatterName(entity.getMatterName());
        sampleBillEntity.setMatterFullName(entity.getMatterName());
        sampleBillEntity.setOperTime(new Date());
        sampleBillService.save(sampleBillEntity,context);
        return super.infoAfter(id, model, entity, context);
    }
}