package xiaoqu.home.open.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xiaoqu.home.open.aop.AvoidRepeatSubmit;
import xiaoqu.home.open.controller.export.ToolsExport;
import xiaoqu.home.open.controller.input.DistrictIdInput;
import xiaoqu.home.open.controller.input.ToolsInsert;
import xiaoqu.home.open.controller.input.ToolsPageInput;
import xiaoqu.home.open.controller.input.task.TaskDistrictParamInsert;
import xiaoqu.home.open.model.db1.District;
import xiaoqu.home.open.model.db1.User;
import xiaoqu.home.open.model.db2.TaskDistrictParam;
import xiaoqu.home.open.model.db2.Tools;
import xiaoqu.home.open.publicBean.BaseOutput;
import xiaoqu.home.open.publicBean.PageOutput;
import xiaoqu.home.open.service.RedisService4;
import xiaoqu.home.open.service.RedisService5;
import xiaoqu.home.open.service.db1.DistrictService;
import xiaoqu.home.open.service.db2.AuthorityService;
import xiaoqu.home.open.service.db2.TaskDistrictParamService;
import xiaoqu.home.open.service.db2.ToolsService;
import xiaoqu.home.open.task.bean.DeleteEntityNotice;
import xiaoqu.home.open.task.bean.NameModifyNotice;
import xiaoqu.home.open.utils.DateToConverter;
import xiaoqu.home.open.utils.ResultResponseBeanUtil;
import xiaoqu.home.open.utils.UserUtils;
import xiaoqu.home.open.utils.WrapperUtils;
import xiaoqu.home.open.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Api(value = "任务站点系统参数")
@Controller
@RequestMapping("/taskDistrictParam")
@Slf4j
public class TaskDistrictParamController {
    @Autowired
    TaskDistrictParamService taskDistrictParamService;
    @Autowired
    RedisService4 redisService4;


    @ApiOperation(value = "任务站点系统参数分页查询")
    @ResponseBody
    @RequestMapping(value = "/taskDistrictParamPageList", method = RequestMethod.POST)
    public BaseOutput<PageOutput<TaskDistrictParam>> taskDistrictParamPageList(HttpServletRequest request, @RequestBody DistrictIdInput input) {
        User user = UserUtils.getUser(request);
        log.info("【taskDistrictParamPageList】input" + JSONObject.toJSONString(input));
        QueryWrapper<TaskDistrictParam> wrapper = WrapperUtils.getWrapper(false);
        PageOutput<TaskDistrictParam> output = new PageOutput<TaskDistrictParam>();
        wrapper = WrapperUtils.beanToWrapper(wrapper, input);
        Page<TaskDistrictParam> pageList = new Page<TaskDistrictParam>(input.getPage(), input.getRows());
        List<TaskDistrictParam> toolsList = taskDistrictParamService.page(pageList, wrapper).getRecords();
//        for (Tools tools:toolsList
//             ) {
//            if(tools.getModifyId()==0){
//                tools.setModifyTime(null);
//            }
//        }
        output.setPage(pageList.getCurrent());
        output.setRecords(pageList.getTotal());
        output.setTotal(pageList.getPages());
        output.setRows(toolsList);
        return ResultResponseBeanUtil.success(output);
    }


    @ApiOperation(value = "修改任务站点系统参数")
    @ResponseBody
    @AvoidRepeatSubmit
    @RequestMapping(value = "/updateTaskDistrictParam", method = RequestMethod.POST)
    public BaseOutput<String> updateTaskDistrictParam(HttpServletRequest request, @RequestBody TaskDistrictParamInsert input) {
        User user = UserUtils.getUser(request);
        if (input.getDistrictId() == null || input.getDistrictId()==0) {
            return ResultResponseBeanUtil.fail("请选择站点");
        }
//        log.info("【addOrUpdateTools】User:" +user.getName()+",参数："+ JSONObject.toJSONString(input));
        TaskDistrictParam taskDistrictParam = taskDistrictParamService.getById(input.getId());
        if(taskDistrictParam!=null){
            BeanUtils.copyProperties(input,taskDistrictParam);
             redisService4.set("PoolOrderCount:1:" + taskDistrictParam.getDistrictId(),taskDistrictParam.getUrgentLimit().toString());
             redisService4.set("PoolOrderCount:2:" + taskDistrictParam.getDistrictId(),taskDistrictParam.getSpecialLimit().toString());
             redisService4.set("PoolOrderCount:3:" + taskDistrictParam.getDistrictId(),taskDistrictParam.getCommonLimit().toString());
            taskDistrictParamService.updateById(taskDistrictParam);
        }else{
            return ResultResponseBeanUtil.fail("数据记录不存在，请刷新页面");

        }

        return ResultResponseBeanUtil.success("编辑成功");



    }



}
