package com.sifang.modules.flowpath.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.flowable.service.ProcessFlowableService;
import com.sifang.modules.flowable.utils.FormUtils;
import com.sifang.modules.flowpath.entity.ExamineInfo;
import com.sifang.modules.flowpath.service.ExamineInfoService;
import com.sifang.modules.flowpath.service.PriceCoefficientApplyService;
import com.sifang.modules.flowpath.vo.PriceVo;
import com.sifang.modules.flowpath.vo.UserApplyVo;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.supply.stock.utils.MaterialUtils;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/apply/priceCoefficient")
public class PriceCoefficientApplyController extends BaseExceptionController {

    public static final Logger logger= LoggerFactory.getLogger(PriceCoefficientApplyController.class);

    @Autowired
    private PriceCoefficientApplyService applyService;

    @Autowired
    private ExamineInfoService examineInfoService;

    @Autowired
    private ProcessFlowableService flowableService;

    //页面
    @RequestMapping(value = "priceCoefficientListPage")
    public String priceCoefficientListPage(Model model) {
        User user = UserUtils.getUser();
        model.addAttribute("userId",user.getId());
        model.addAttribute("formId", FormUtils.PRICE_COEFFICIENT_APPLY);
        return "modules/flowpath/priceCoefficientListPage";
    }

    @ResponseBody
    @RequestMapping(value = "listData")
    public String listData(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        applyService.applyList(page,map);
        return page.renderJson();
    }

    //新增页面
    @RequestMapping(value = "addPriceCoefficientApplyPage")
    public String addPriceCoefficientApplyPage(Model model) {
        //获取物料类型
        model.addAttribute("types", MaterialUtils.findMaterialType());
        return "modules/flowpath/addPriceCoefficientApplyPage";
    }

    //新建/修改申请
    @ResponseBody
    @RequestMapping(value = "savePriceCoefficientApply")
    public Result saveDistributionGoods(@RequestBody PriceVo vo){
        try{
            applyService.savePriceCoefficientApply(vo);
            return Result.ok();
        }catch (RuntimeException r){
            r.printStackTrace();
            return Result.fail(r.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>>savePriceCoefficientApply e:{}",e);
        }
        return Result.fail("保存失败");
    }

    //编辑页面
    @RequestMapping(value = "editPriceCoefficientApplyPage")
    public String editPriceCoefficientApplyPage(HttpServletRequest request,Model model) {
        //获取物料类型
        model.addAttribute("types", MaterialUtils.findMaterialType());
        String id = request.getParameter("id");
        Record apply = applyService.getDetailById(id);
        model.addAttribute("apply", apply);
        return "modules/flowpath/addPriceCoefficientApplyPage";
    }

    //查看详情
    @RequestMapping(value = "priceCoefficientApplyDetail")
    public String priceCoefficientApplyDetail(HttpServletRequest request,Model model){
        String id=request.getParameter("id");
        String from=request.getParameter("from"); //from = audit 那么就是审批操作  否则就是查看
        model.addAttribute("from",from);
        Record apply=applyService.getDetailById(id);
        model.addAttribute("apply",apply);
        List<ExamineInfo> infos=examineInfoService.getByApplyId(id,FormUtils.PRICE_COEFFICIENT_APPLY);
        model.addAttribute("infos",infos);
        User user = UserUtils.getUser();
        if(!"audit".equals(from)){
            flowableService.readApplication(apply.getStr("process_instance_id"),FormUtils.PRICE_COEFFICIENT_APPLY,id,user);
        }
        return "modules/flowpath/priceCoefficientApplyDetail";
    }

    //删除申请
    @ResponseBody
    @RequestMapping(value = "deletePriceCoefficientApply")
    public Result deletePriceCoefficientApply(HttpServletRequest request){
        String id=request.getParameter("id");
        try{
            applyService.deletePriceCoefficientApply(id);
            return Result.ok();
        }catch (RuntimeException r){
            r.printStackTrace();
            return Result.fail(r.getMessage());
        }catch (Exception e){
            logger.error(">>>>>>>>>>>>>>deletePriceCoefficientApply e:{}",e);
            e.printStackTrace();
            return Result.fail("删除失败！");
        }
    }

    //审核通过
    @ResponseBody
    @RequestMapping(value = "examinePriceCoefficientApplyApprove")
    public Result examinePriceCoefficientApplyApprove(UserApplyVo userApplyVo){
        try{
            applyService.examinePriceCoefficientApplyApprove(userApplyVo.getId(),userApplyVo.getTaskId(),userApplyVo.getContent());
            return Result.ok();
        }catch (RuntimeException c){
            logger.info(">>>>>>>>>>>>>>examinePriceCoefficientApplyApprove c:{}",c);
            return Result.fail(c.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>examinePriceCoefficientApplyApprove e:{}",e);
            return Result.fail("审核失败！");
        }
    }

    //审核驳回
    @ResponseBody
    @RequestMapping(value = "rejectPriceCoefficientApplyTask")
    public Result rejectPriceCoefficientApplyTask(@Valid UserApplyVo userApplyVo){
        try{
            applyService.rejectPriceCoefficientApplyTask(userApplyVo.getId(),userApplyVo.getTaskId(),userApplyVo.getContent());
            return Result.ok();
        }catch (Exception e){
            logger.error(">>>>>>>>>>>>>>rejectPriceCoefficientApplyTask e:{}",e);
            return Result.fail("审核失败！");
        }
    }
}
