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

import com.td.boss.annotation.Decrypt;
import com.td.boss.annotation.Encrypt;
import com.td.boss.common.controller.CommonController;
import com.td.boss.common.pojo.PageCondition;
import com.td.boss.common.pojo.PageInfo;
import com.td.boss.common.pojo.Result;
import com.td.boss.game.comcntorder.service.ComCntOrderService;
import com.td.boss.game.comcntorder.vo.ComCntOrderDateVo;
import com.td.boss.game.comcntorder.vo.ComCntOrderPayTypeSumVo;
import com.td.boss.game.comcntorder.vo.ComCntOrderVo;
import com.td.boss.game.comcnttosnb.pojo.ComCntToSnb;
import com.td.boss.game.comcnttosnb.service.ComCntToSnbService;
import com.td.boss.game.comcnttosnb.vo.ComCntToSnbVo;
import com.td.boss.game.comconfigland.pojo.ComConfigLand;
import com.td.boss.game.comconfigland.service.ComConfigLandService;
import com.td.boss.game.comconfigland.vo.ComConfigLandVo;
import com.td.boss.game.comfruit.pojo.ComFruit;
import com.td.boss.game.comfruit.service.ComFruitService;
import com.td.boss.game.comfruit.vo.ComFruitVo;
import com.td.boss.game.commallseed.pojo.ComMallSeed;
import com.td.boss.game.commallseed.service.ComMallSeedService;
import com.td.boss.game.commallseed.vo.ComMallSeedVo;
import com.td.boss.game.complayergoods.service.ComPlayerGoodsService;
import com.td.boss.game.complayergoods.vo.ComPlayerGoodsTypeSumVo;
import com.td.boss.game.comsnbapply.pojo.ComSnbApply;
import com.td.boss.game.comsnbapply.service.ComSnbApplyService;
import com.td.boss.game.comsnbapply.vo.ComSnbApplyBetweenVo;
import com.td.boss.game.comsnbapply.vo.ComSnbApplyVo;
import com.td.boss.game.comsnbtran.service.ComSnbTranService;
import com.td.boss.game.comsnbtran.vo.ComSnbTranAbnormalVo;
import com.td.boss.game.comsnbtran.vo.ComSnbTranDateVo;
import com.td.boss.game.comsnbtran.vo.ComSnbTranTranTypeSumVo;
import com.td.boss.game.comusers.pojo.ComUsers;
import com.td.boss.game.comusers.service.ComUsersService;
import com.td.boss.game.comusers.vo.ComUsersVo;
import com.td.boss.sys.sysGame.vo.SysApplySNBToCNTVo;
import com.td.boss.util.CopyUtil;
import com.td.boss.util.SysSettingTaskUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@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;


    @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",
                SysSettingTaskUtil.getSysSettingTask());
        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));
    }
}
