package com.sifang.modules.completionrectification.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.dao.CompletionDao;
import com.sifang.modules.completionrectification.entity.CrmRectificationInfo;
import com.sifang.modules.completionrectification.service.CompletionService;
import com.sifang.modules.completionrectification.service.RectificationInfoDetailService;
import com.sifang.modules.completionrectification.service.RectificationService;
import com.sifang.modules.completionrectification.vo.CrmCompletionInfoVo;
import com.sifang.modules.completionrectification.vo.CrmRectificationVo;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
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.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 java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "${adminPath}/rectification")
public class RectificationController extends BaseExceptionController {

    @Autowired
    private RectificationService rectificationService;

    @Autowired
    private RectificationInfoDetailService rectificationInfoDetailService;

    @Autowired
    AddressService addressService;

    @Autowired
    TraderService traderService;

    @Autowired
    CompletionDao completionDao;

    @RequestMapping(value = "rectificationOrder")
    public String rectificationOrder(Model model, HttpServletRequest request){
        //代理商列表
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        User user = UserUtils.getUser();
        String userType = user.getUserType();
        model.addAttribute("userType", userType);
        return "modules/yangguang/completionRectification/rectificationOrder";
    }

    @ResponseBody
    @RequestMapping("/getRectificationOrderList")
    public Object getRectificationOrderList(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> map = getParams(request);
        rectificationService.getRectificationOrderList(page, map);
        return page.renderJson();
    }

    @RequestMapping(value = "addRectification")
    public String addRectification(Model model, HttpServletRequest request){
        String id = request.getParameter("id");
        CrmRectificationInfo reInfo = rectificationService.findRectificationById(id);
        List<Record> list = rectificationService.getRectificationOrderStationList(reInfo.getInputPieceId());
        String idList = rectificationInfoDetailService.getRectificationDetailIdStr(reInfo.getRectificationNo());
        model.addAttribute("list", list);
        model.addAttribute("reInfo", reInfo);
        model.addAttribute("idList", idList);
        return "modules/yangguang/completionRectification/rectification";
    }

    @RequestMapping(value = "rectificationDetail")
    public String rectificationDetail(Model model, HttpServletRequest request){
        String id = request.getParameter("id");
        CrmRectificationInfo reInfo = rectificationService.findRectificationById(id);
        List<Record> list = rectificationService.getRectificationOrderStationList(reInfo.getInputPieceId());
        Record info = rectificationService.getRectificationOrderDetail(id);
        model.addAttribute("list", list);
        model.addAttribute("info", info);
        model.addAttribute("reInfo", reInfo);
        return "modules/yangguang/completionRectification/rectificationDetail";
    }

    @RequestMapping(value = "rectificationResult")
    public String rectificationResult(Model model, HttpServletRequest request){
        //代理商列表
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        return "modules/yangguang/completionRectification/rectificationResult";
    }

    @ResponseBody
    @RequestMapping("/getRectificationResultList")
    public Object getRectificationResultList(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> map = getParams(request);
        rectificationInfoDetailService.getRectificationResultList(page, map);
        return page.renderJson();
    }

    /**
     * 整改提交
     */
    @ResponseBody
    @RequestMapping("/saveRectificationInfo")
    public Result saveRectificationInfo(@RequestBody String str){
        JSONObject jsonObject = JSONObject.parseObject(str);
        return rectificationService.saveRectificationInfo(jsonObject);
    }

}