package com.sifang.modules.sunshine.settleSet.controller;

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
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.sunshine.settleSet.entity.CrmRewardsPunishWay;
import com.sifang.modules.sunshine.settleSet.service.RewardsPunishWayService;
import com.sifang.modules.sunshine.settleSet.vo.RewardWayVo;
import com.sifang.modules.sunshine.settleSet.vo.SaveRewardVo;
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.validation.annotation.Validated;
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;

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

    @Autowired
    private RewardsPunishWayService rewardsPunishWayService;

    @Autowired
    private TraderService traderService;


    //奖励政策
    @RequestMapping(value = "rewardsWayPage")
    public String rewardsWayPage(Model model){
        List<Record> traders=traderService.getAllTrader();
        model.addAttribute("traders",traders);
        return "modules/setting/settleSet/rewardsWayPage";
    }

    //惩罚政策
    @RequestMapping(value = "punishWayPage")
    public String punishWayPage(){
        return "modules/setting/settleSet/punishWayPage";
    }

    //列表数据
    @ResponseBody
    @RequestMapping(value = "rewardsPunishWayListDate")
    public Object rewardsPunishWayListDate(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage<Record> page = new LayuiPage<>(request);
        rewardsPunishWayService.rewardsPunishWayListDate(map,page);
        return page.renderJson();
    }

    @ResponseBody
    @RequestMapping(value = "punishWayListDate")
    public Object punishWayListDate(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        List<Record> list= rewardsPunishWayService.punishWayListDate();
        page.setData(list);
        return page.renderJson();
    }

    //保存惩罚政策页面
    @RequestMapping(value = "savePunishWayPage")
    public String saveRewardsWayPage(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        CrmRewardsPunishWay punishWay=rewardsPunishWayService.getById(id);
        model.addAttribute("punishWay",punishWay);
        return "modules/setting/settleSet/savePunishWayPage";
    }

    //保存奖励政策页面
    @RequestMapping(value = "addRewardsWayPage")
    public String addRewardsWayPage(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        String number="";
        String traderIds="-1";
        if(StringUtil.isNotBlank(id)){
            CrmRewardsPunishWay punishWay=rewardsPunishWayService.getById(id);
            model.addAttribute("punishWay",punishWay);
            number=punishWay.getNumber();
            if(StringUtil.isNotBlank(punishWay.getTraderIds())){
                traderIds=punishWay.getTraderIds();
            }
            Map<String,String> map = Maps.newHashMap();
            map.put("traderIds",traderIds);
            model.addAttribute("traders",traderService.findTraderRecordsList(map));
        }else{
            //获取最大的version,然后生成方案编号
            number=rewardsPunishWayService.createNumber();
        }
        model.addAttribute("traderIds",traderIds);
        model.addAttribute("number",number);
        return "modules/setting/settleSet/addRewardsWayPage";
    }

    //保存设置--保存惩罚政策
    @ResponseBody
    @RequestMapping(value = "savePunishWay")
    public Result savePunishWay(@RequestBody @Validated SaveRewardVo saveRewardVo){
        rewardsPunishWayService.savePunishWay(saveRewardVo);
        return Result.ok();
    }

    //保存设置--奖励政策
    @ResponseBody
    @RequestMapping(value = "saveRewardsWay")
    public Result saveRewardsWay(@RequestBody RewardWayVo rewardWayVo){
        rewardsPunishWayService.saveRewardsWay(rewardWayVo);
        return Result.ok();
    }

    @RequestMapping(value = "getChangeRecord")
    public String getChangeRecord(HttpServletRequest request,Model model){
        String id=request.getParameter("id");
        List<Record> msgs=rewardsPunishWayService.getChangeRecord(id);
        model.addAttribute("msgs",msgs);
        return "modules/setting/settleSet/getChangeRecord";
    }
}
