package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.system.api.domain.SysRole;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.cecep.talroad.vo.query.FactoryReportingRemindersQuery;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.security.InvalidParameterException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;

import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.domain.FactoryReportingReminders;
import cn.cecep.talroad.vo.FactoryReportingRemindersVo;
import cn.cecep.talroad.service.IFactoryReportingRemindersService;


/**
 * * 开发公司：中节能天融科技有限公司
 * 企业信息上报提交提示Controller 控制层
 *
 * @author szhpt
 * @date 2023-05-09*/
@Api(tags = "企业信息上报提交提示")
@RestController
@RequestMapping("/reminders")
public class FactoryReportingRemindersController extends BasesController
{
    @Autowired
    private IFactoryReportingRemindersService factoryReportingRemindersService;

    /**
     * 查询企业信息上报提交提示列表
     */
    @ApiOperation(value = "查询企业信息上报提交提示")
    @GetMapping("/list")
    public AjaxResults<IPage<FactoryReportingRemindersVo>> list(FactoryReportingRemindersQuery query)
    {
        Page<FactoryReportingRemindersVo> page = new Page<>(query.getPageNum(), query.getPageSize());

        List<SysRole> roleList = SecurityUtils.getLoginUser().getSysUser().getRoles();
        List<Long> roleIdList = roleList.stream().map(SysRole::getRoleId).collect(Collectors.toList());
        if(roleIdList.size()>0){
            for(Long roleId:roleIdList){
                if(ObjectUtil.equal(roleId,9L)){
                    String regionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
                    query.setRegionCode(regionCode);
                }
            }
        }
        factoryReportingRemindersService.factoryReportingRemindersList(page, query);
        return AjaxResults.success(page);
    }
    @ApiOperation(value = "初始化企业信息上报状态")
    @GetMapping("/initReminders")
    public void initReminders(FactoryReportingRemindersQuery factoryReportingReminders)
    {
        factoryReportingRemindersService.factoryReportingReminders();
    }


    /**
     * 导出企业信息上报提交提示列表
     */
    @ApiOperation(value = "导出企业信息上报提交提示")
    @Log(title = "企业信息上报提交提示", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FactoryReportingReminders factoryReportingReminders)
    {
        List<FactoryReportingReminders> list = factoryReportingRemindersService.selectFactoryReportingRemindersList(factoryReportingReminders);
        ExcelUtil<FactoryReportingReminders> util = new ExcelUtil<FactoryReportingReminders>(FactoryReportingReminders.class);
        util.exportExcel(response, list, "企业信息上报提交提示数据");
    }

    /**
     * 获取企业信息上报提交提示详细信息
     */
    @ApiOperation(value = "获取企业信息上报提交提示详细信息")
    @PostMapping(value = "/{factoryId}")
    public AjaxResults<FactoryReportingReminders> getInfo(@PathVariable("factoryId") String factoryId) {
        return AjaxResults.success(factoryReportingRemindersService.getOne(new QueryWrapper<FactoryReportingReminders>().eq("factory_id",factoryId)));
    }
    /**
     * 新增企业信息上报提交提示
     */
    @ApiOperation(value = "新增企业信息上报提交提示")
    @Log(title = "企业信息上报提交提示", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody FactoryReportingReminders factoryReportingReminders) {
        return toAjax(factoryReportingRemindersService.save(factoryReportingReminders));
    }
    /**
     * 修改企业信息上报提交提示
     */
    @ApiOperation(value = "修改企业信息上报提交提示")
    @Log(title = "企业信息上报提交提示", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody FactoryReportingRemindersVo factoryReportingRemindersVo) {
        if(ObjectUtil.isEmpty(factoryReportingRemindersVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(factoryReportingRemindersVo.getFactoryId())){
            throw new InvalidParameterException("企业id为空");
        }

        FactoryReportingRemindersVo factoryReportingReminders =new FactoryReportingRemindersVo();
        factoryReportingReminders.setFactoryId(factoryReportingRemindersVo.getFactoryId());

        factoryReportingReminders.setSubmitTime(
                DateUtil.parse(LocalDateTime.now().format(
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        ),"yyyy-MM-dd HH:mm:ss"));
//        factoryReportingReminders.setSubmitUser(SecurityUtils.getUsername());
        Integer bol = factoryReportingRemindersService.updateReminder(factoryReportingReminders);
        return AjaxResults.success("提交成功",true);
    }

    /**
     * 删除企业信息上报提交提示
     */
//    @ApiOperation(value = "删除企业信息上报提交提示")
//    @RequiresPermissions("talroad:reminders:remove")
//    @Log(title = "企业信息上报提交提示", businessType = BusinessType.DELETE)
//    @PostMapping("/{feIds}")
//    public AjaxResults<Boolean> remove(@PathVariable String[] feIds) {
//        return toAjax(factoryReportingRemindersService.removeByIds(Arrays.asList(feIds)));
//    }
}
