package com.td.WaDa.sys.sysGame.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.td.WaDa.annotation.Decrypt;
import com.td.WaDa.annotation.Encrypt;
import com.td.WaDa.common.controller.CommonController;
import com.td.WaDa.common.pojo.*;
import com.td.WaDa.common.service.ComConfigService;
import com.td.WaDa.game.comcntorder.service.ComCntOrderService;
import com.td.WaDa.game.comcntorder.vo.ComCntOrderDateVo;
import com.td.WaDa.game.comcntorder.vo.ComCntOrderPayTypeSumVo;
import com.td.WaDa.game.comcntorder.vo.ComCntOrderVo;
import com.td.WaDa.game.comcnttosnb.pojo.ComCntToSnb;
import com.td.WaDa.game.comcnttosnb.service.ComCntToSnbService;
import com.td.WaDa.game.comcnttosnb.vo.ComCntToSnbVo;
import com.td.WaDa.game.comconfigland.pojo.ComConfigLand;
import com.td.WaDa.game.comconfigland.service.ComConfigLandService;
import com.td.WaDa.game.comconfigland.vo.ComConfigLandVo;
import com.td.WaDa.game.comfruit.pojo.ComFruit;
import com.td.WaDa.game.comfruit.service.ComFruitService;
import com.td.WaDa.game.comfruit.vo.ComFruitVo;
import com.td.WaDa.game.commallseed.pojo.ComMallSeed;
import com.td.WaDa.game.commallseed.service.ComMallSeedService;
import com.td.WaDa.game.commallseed.vo.ComMallSeedVo;
import com.td.WaDa.game.complayercultivate.vo.ComPlayerCultivateSeedVo;
import com.td.WaDa.game.complayergoods.service.ComPlayerGoodsService;
import com.td.WaDa.game.complayergoods.vo.ComPlayerGoodsTypeSumVo;
import com.td.WaDa.game.complayersattri.vo.ComPlayersLuckyLandLevelUpVo;
import com.td.WaDa.game.complayersattri.vo.ComPlayersLuckyToolsVo;
import com.td.WaDa.game.comsetting.service.ComSettingService;
import com.td.WaDa.game.comsnbapply.pojo.ComSnbApply;
import com.td.WaDa.game.comsnbapply.service.ComSnbApplyService;
import com.td.WaDa.game.comsnbapply.vo.ComSnbApplyBetweenVo;
import com.td.WaDa.game.comsnbapply.vo.ComSnbApplyVo;
import com.td.WaDa.game.comsnbtran.service.ComSnbTranService;
import com.td.WaDa.game.comsnbtran.vo.ComSnbTranAbnormalVo;
import com.td.WaDa.game.comsnbtran.vo.ComSnbTranDateVo;
import com.td.WaDa.game.comsnbtran.vo.ComSnbTranTranTypeSumVo;
import com.td.WaDa.game.comusers.pojo.ComUsers;
import com.td.WaDa.game.comusers.service.ComUsersService;
import com.td.WaDa.game.comusers.vo.ComUsersVo;
import com.td.WaDa.sys.sysGame.vo.SysApplySNBToCNTVo;
import com.td.WaDa.util.CopyUtil;
import com.td.WaDa.util.RedisSettingMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/sys/sysProperty/")
public class sysGamePropertyController extends CommonController<ComUsersVo, ComUsers, String> {
    @Autowired
    private ComUsersService comUsersService;

    @Autowired
    private ComPlayerGoodsService comPlayerGoodsService;

    @Autowired
    private ComSnbTranService comSnbTranService;

    @Autowired
    private ComCntOrderService comCntOrderService;

    @Autowired
    private ComMallSeedService comMallSeedService;

    @Autowired
    private ComConfigLandService comConfigLandService;

    @Autowired
    private ComFruitService comFruitService;


    @Autowired
    private ComSnbApplyService comSnbApplyService;

    @Autowired
    private ComCntToSnbService comCntToSnbService;

    @Autowired
    private ComSettingService comSettingService;

    @Autowired
    private ComConfigService comConfigService;

    @Autowired
    private RedisSettingMap redisSettingMap;


    @GetMapping("CNTAndSNB")
    public ModelAndView CNTAndSNB() {

        return new ModelAndView("sys/aComCNTAnbSNB/CNTAndSNB");
    }


    //土地租赁消耗总CNT数量，今日新增土地租赁消耗CNT数量
    @PostMapping("findAllCNTGroupByPayType")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComCntOrderPayTypeSumVo>> findAllCNTGroupByPayType(ComCntOrderVo comCntOrderVo) {
        List<ComCntOrderPayTypeSumVo> comCntOrderPayTypeSumVoList = comCntOrderService.findAllCNTSumGroupByPayType();
        PageInfo<ComCntOrderPayTypeSumVo> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(5);//页面大小
        pageInfo.setRows(comCntOrderPayTypeSumVoList);//分页结果
        pageInfo.setRecords(comCntOrderPayTypeSumVoList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }

    @PostMapping("findAllCNTSumGroupByPayTypeFromDate")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComCntOrderPayTypeSumVo>> findAllCNTSumGroupByPayTypeFromDate(ComCntOrderDateVo comCntOrderDateVo) {
        List<ComCntOrderPayTypeSumVo> comCntOrderPayTypeSumVoList = comCntOrderService.findAllCNTSumGroupByPayTypeFromDate(comCntOrderDateVo.getStartTime(), comCntOrderDateVo.getEndTime());
        PageInfo<ComCntOrderPayTypeSumVo> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(5);//页面大小
        pageInfo.setRows(comCntOrderPayTypeSumVoList);//分页结果
        pageInfo.setRecords(comCntOrderPayTypeSumVoList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }

    //snb购买种子的总消耗
    @PostMapping("findAllSNBGroupByPayType")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComSnbTranTranTypeSumVo>> findAllSNBGroupByPayType(ComCntOrderVo comCntOrderVo) {
        List<ComSnbTranTranTypeSumVo> comSnbTranTranTypeSumVoList = comSnbTranService.findAllSNBSumGroupByPayType();
        PageInfo<ComSnbTranTranTypeSumVo> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(5);//页面大小
        pageInfo.setRows(comSnbTranTranTypeSumVoList);//分页结果
        pageInfo.setRecords(comSnbTranTranTypeSumVoList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }


    @PostMapping("findAllSNBErrorInfo")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComSnbTranAbnormalVo>> findAllSNBErrorInfo(ComCntOrderVo comCntOrderVo) {
        List<ComSnbTranAbnormalVo> comSnbTranTranTypeSumVoList = comSnbTranService.findAllComSnbTranAbnormal();
        PageInfo<ComSnbTranAbnormalVo> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(5);//页面大小
        pageInfo.setRows(comSnbTranTranTypeSumVoList);//分页结果
        pageInfo.setRecords(comSnbTranTranTypeSumVoList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }

    @PostMapping("findAllSNBSumGroupByPayTypeFromDate")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComSnbTranTranTypeSumVo>> findAllSNBSumGroupByPayTypeFromDate(ComSnbTranDateVo comSnbTranDateVo) {
        List<ComSnbTranTranTypeSumVo> comSnbTranTranTypeSumVoList = comSnbTranService.findAllSNBSumGroupByPayTypeFromDate(comSnbTranDateVo.getStartTime(), comSnbTranDateVo.getEndTime());
        PageInfo<ComSnbTranTranTypeSumVo> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(5);//页面大小
        pageInfo.setRows(comSnbTranTranTypeSumVoList);//分页结果
        pageInfo.setRecords(comSnbTranTranTypeSumVoList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }

    @GetMapping("playerGoods")
    public ModelAndView playerGoods() {

        return new ModelAndView("sys/aComPlayerGoods/playerGoods");
    }

    @PostMapping("findAllGroupByGoodsType")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComPlayerGoodsTypeSumVo>> findAllGroupByGoodsType() {
        List<ComPlayerGoodsTypeSumVo> comPlayerGoodsTypeSumVoList = comPlayerGoodsService.findAllGroupByGoodsType();
        PageInfo<ComPlayerGoodsTypeSumVo> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(5);//页面大小
        pageInfo.setRows(comPlayerGoodsTypeSumVoList);//分页结果
        pageInfo.setRecords(comPlayerGoodsTypeSumVoList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }


    //获取种子设置页面
    @GetMapping("seedConfig")
    public ModelAndView seedConfig() {
        return new ModelAndView("sys/aComConfig/seedConfig");
    }

    //todo 后续种子多需要扩展，这里暂时这样写
    @PostMapping("findAllSeedConfig")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComMallSeed>> findAllSeedConfig() {
        List<ComMallSeed> comMallSeedList = comMallSeedService.findMallSeedAll();
        PageInfo<ComMallSeed> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(10);//页面大小
        pageInfo.setRows(comMallSeedList);//分页结果
        pageInfo.setRecords(comMallSeedList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }

    @PostMapping("saveSeedConfig")
    @Decrypt
    @Encrypt
    public Result saveSeedConfig(ComMallSeedVo comMallSeedVo) {
        return comMallSeedService.save(comMallSeedVo);
    }


    //获取土地设置页面
    @GetMapping("landConfig")
    public ModelAndView landConfig() {
        return new ModelAndView("sys/aComConfig/landConfig");
    }

    @PostMapping("findAllLandConfig")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComConfigLand>> findAllLandConfig() {
        List<ComConfigLand> comConfigLandList = comConfigLandService.findConfigLandAll();
        PageInfo<ComConfigLand> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(comConfigLandList.size());//页面大小
        pageInfo.setRows(comConfigLandList);//分页结果
        pageInfo.setRecords(comConfigLandList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }

    @PostMapping("saveLandConfig")
    @Decrypt
    @Encrypt
    public Result saveLandConfig(ComConfigLandVo comConfigLandVo) {
        return comConfigLandService.save(comConfigLandVo);
    }

    //获取果实销售价格
    @GetMapping("fruitConfig")
    public ModelAndView fruitConfig() {
        return new ModelAndView("sys/aComConfig/fruitConfig");
    }

    @PostMapping("findAllFruitConfig")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComFruit>> findAllFruitConfig() {
        List<ComFruit> comFruitList = comFruitService.findFruitAll();
        PageInfo<ComFruit> pageInfo = new PageInfo<>();
        pageInfo.setPage(1);//页码
        pageInfo.setPageSize(comFruitList.size());//页面大小
        pageInfo.setRows(comFruitList);//分页结果
        pageInfo.setRecords(comFruitList.size());//总记录数
        pageInfo.setTotal(1);//总页数
        return Result.of(pageInfo);
    }

    @PostMapping("saveFruitConfig")
    @Decrypt
    @Encrypt
    public Result saveFruitConfig(ComFruitVo comFruitVo) {
        return comFruitService.save(comFruitVo);
    }

    /**
     * 审核SNB部分
     */
    @GetMapping("applySnbToCntView")
    public ModelAndView usersApplySnbToCnt() {
        ModelAndView modelAndView = new ModelAndView("sys/aComApplySNBToCNT/applySNBToCNT", "sysSettingTask",
                redisSettingMap.getSysSettingTaskMap());
        modelAndView.getModelMap().addAttribute("applyState", 0);
        return modelAndView;
    }


    @PostMapping("findApplySnbToCntPage")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComSnbApplyVo>> findUsersApplySnbToCnt(ComSnbApplyVo comSnbApplyVo) {
        return comSnbApplyService.page(comSnbApplyVo);
    }


    @PostMapping("updateApplySnbToCntData")
    @Decrypt
    @Encrypt
    public Result<ComSnbApplyVo> updateApplySnbToCntData(ComSnbApplyVo comSnbApplyVo) {
        comSnbApplyVo.setUpdateTime(new Date());
        return comSnbApplyService.save(comSnbApplyVo);
    }

    @PostMapping("updateApplySnbToCntDataList")
    @Decrypt
    @Encrypt
    public Result<String> updateApplySnbToCntDataList(SysApplySNBToCNTVo sysApplySNBToCNTVo) {
        //再保存新的
        List<ComSnbApply> comSnbApplies = comSnbApplyService.findAllByIn(Arrays.asList(sysApplySNBToCNTVo.getApplyIdList().split(",")));
        for (ComSnbApply comSnbApply : comSnbApplies) {
            if (!comSnbApply.getIsState().equals(3)) {
                comSnbApply.setIsState(sysApplySNBToCNTVo.getIsState());
                comSnbApply.setUpdateTime(new Date());
            }
        }
        comSnbApplyService.saveAll(comSnbApplies);
        return Result.of("success");
    }

    @PostMapping("findBetweenApplySnbToCntDataPage")
    @Decrypt
    @Encrypt
    public Result<List<ComSnbApplyVo>> findBetweenApplySnbToCntDataPage(ComSnbApplyBetweenVo comSnbApplyBetweenVo) {
        List<ComSnbApplyVo> comSnbApplyVoList = CopyUtil.copyList(comSnbApplyService.getBetweenTimeList(comSnbApplyBetweenVo.getBeginTime(), comSnbApplyBetweenVo.getEndTime()), ComSnbApplyVo.class);
        return Result.of(comSnbApplyVoList);
    }


    //领取snb页面
    @GetMapping("comCntToSnbView")
    public ModelAndView comCntToSnbView() {
        ModelAndView modelAndView = new ModelAndView("sys/aComCntToSnb/aComCntToSnb");
        return modelAndView;
    }

    @PostMapping("findComCntToSnbPage")
    @Decrypt
    @Encrypt
    public Result<PageInfo<ComCntToSnbVo>> findComCntToSnbPage(ComCntToSnbVo comCntToSnbVo) {
        return comCntToSnbService.page(comCntToSnbVo);
    }

    @PostMapping("findBetweenComCntToSnbPage")
    @Decrypt
    @Encrypt
    public Result<List<ComCntToSnb>> findBetweenComCntToSnbPage(
            @RequestParam(value = "beginTime") Date beginTime,
            @RequestParam(value = "endTime") Date endTime) {
        return Result.of(comCntToSnbService.getBetweenTimeList(beginTime, endTime));
    }

    /**
     * 返回一个应用设置
     *
     * @return
     */
    @GetMapping("gameConfig")
    public ModelAndView gameConfig() {
        //ComSettingVo comSettingVo =  comSettingService.get("1").getData();
        return new ModelAndView("sys/aComConfig/gameConfig", "gameConfig", redisSettingMap.getComSettingMap());
    }


    /**
     * 获取全部配置
     */
    @GetMapping("comConfig")
    public ModelAndView ComConfig() {
        List<ComConfig> comConfigs = comConfigService.selectAllComConfig();

        Map map = new HashMap();
        for (ComConfig c : comConfigs) {
            if (c.getConfigKey().equals("cultivate_food")) {
                List<ComPlayerCultivateSeedVo> cultivateFoodList = JSONUtil.toList(c.getConfigValue(), ComPlayerCultivateSeedVo.class);
                map.put("CultivateFoodList", cultivateFoodList);
            }else if (c.getConfigKey().equals("land_level")) {
                List<ComPlayersLuckyLandLevelUpVo> LandLevelList = JSONUtil.toList(c.getConfigValue(), ComPlayersLuckyLandLevelUpVo.class);
                map.put("LandLevelList", LandLevelList);
            }else if (c.getConfigKey().equals("land_luck_tosnb")) {
                //兑换snb倍数
                int snb = Convert.toInt(c.getConfigValue(), 0);
                map.put("snbMultiple", snb);
            }else if (c.getConfigKey().equals("land_tools")) {
                List<ComPlayersLuckyToolsVo> comPlayersLuckyToolsVos = JSONUtil.toList(c.getConfigValue(), ComPlayersLuckyToolsVo.class);
                map.put("LuckyTools", comPlayersLuckyToolsVos);
            } else if (c.getConfigKey().equals("max_amount")) {
                //获取配置 土堆分别最大种植数量，对应类型mallType：1、2、3、4的值
                // 1、牧场 2、养殖 3、渔场淡水区 4、渔场海水区
                List<Map> mapList = JSONUtil.toList(c.getConfigValue(), Map.class);
                map.put("MaxAmountList", mapList);
            } else if (c.getConfigKey().equals(CultivateKeys.witheredDay)) {
                //todo [枯萎]
                Integer witheredDay = comConfigService.selectByKey(CultivateKeys.witheredDay, Integer.class);
                map.put("witheredDay", witheredDay);
            }
        }

        return new ModelAndView("sys/aComConfig/comConfig", "comConfig", map);
    }
}
