package org.jeecg.modules.system.app.controller;

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.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.app.entity.StatisticsInfo;
import org.jeecg.modules.system.app.service.IStatisticsInfoService;
import org.jeecg.modules.system.app.vo.statistics.FindByShopSumVO;
import org.jeecg.modules.system.app.vo.statistics.FindByStatisticsChartVO;
import org.jeecg.modules.system.app.vo.statistics.FindByStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 首页统计表
 * @Author: jeecg-boot
 * @Date:   2025-08-18
 * @Version: V1.0
 */
@Tag(name="首页统计表")
@RestController
@RequestMapping("/statisticsInfo")
@Slf4j
public class StatisticsInfoController extends JeecgController<StatisticsInfo, IStatisticsInfoService> {
	@Autowired
	private IStatisticsInfoService statisticsInfoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param statisticsInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "首页统计表-分页列表查询")
	@Operation(summary="首页统计表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<StatisticsInfo>> queryPageList(StatisticsInfo statisticsInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {


        QueryWrapper<StatisticsInfo> queryWrapper = QueryGenerator.initQueryWrapper(statisticsInfo, req.getParameterMap());
		Page<StatisticsInfo> page = new Page<StatisticsInfo>(pageNo, pageSize);
		IPage<StatisticsInfo> pageList = statisticsInfoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 /**
	  * 首页查询
	  * @return
	  */
	 //@AutoLog(value = "首页统计表-分页列表查询")
	 @Operation(summary="首页统计表-首页查询")
	 @GetMapping(value = "/findByStatistics")
	 public Result<FindByStatisticsVO> findByStatistics() {
		 LocalDateTime today = LocalDateTime.now();
		 /**获取当天的yyyyMMdd时间格式 int类型*/
		 int dateAsInt = Integer.parseInt(
				 today.format(DateTimeFormatter.ofPattern("yyyyMMdd"))
		 );
		 LocalDateTime yesterday = today.minusDays(1);

		 int yesterdayNum =  Integer.parseInt(
				 yesterday.format(DateTimeFormatter.ofPattern("yyyyMMdd"))
		 );
		 /**获取当月的yyyyMMdd时间格式 int类型*/
		 int firstDayInt = Integer.parseInt(today.withDayOfMonth(1).format(DateTimeFormatter.ofPattern("yyyyMMdd")));
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 FindByStatisticsVO findByStatisticsVOS =  statisticsInfoService.findByStatistics(loginUser.getOrgCode(),dateAsInt,firstDayInt,yesterdayNum);
		 return Result.OK(findByStatisticsVOS);
	 }

	/**
	 * 首页详情查询
	 * @return
	 */
	//@AutoLog(value = "首页统计表-分页列表查询")
	@Operation(summary="首页统计表-首页详情查询")
	@GetMapping(value = "/findByShopMoneyList")
	public Result<List<FindByStatisticsChartVO>> findByShopMoneyList(@RequestParam("type") Integer type) {

		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<FindByStatisticsChartVO> findByStatisticsVOS= statisticsInfoService.findByShopMoneyList(loginUser.getOrgCode(),type);
		return Result.OK(findByStatisticsVOS);
	}
	/**
	 * 首页售卖商品数详情查询
	 * @return
	 */
	//@AutoLog(value = "首页统计表-分页列表查询")
	@Operation(summary="首页统计表-首页售卖商品数详情查询")
	@GetMapping(value = "/findByShopSumList")
	public Result<List<FindByShopSumVO>> findByShopSumList(@RequestParam("type") Integer type) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<FindByShopSumVO> findByStatisticsVOS= statisticsInfoService.findByShopSumList(loginUser.getOrgCode(),type);
		return Result.OK(findByStatisticsVOS);
	}

	
	/**
	 *   添加
	 *
	 * @param statisticsInfo
	 * @return
	 */
	@AutoLog(value = "首页统计表-添加")
	@Operation(summary="首页统计表-添加")
	@RequiresPermissions("statisticsInfo:nm_statistics_info:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StatisticsInfo statisticsInfo) {
		statisticsInfoService.save(statisticsInfo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param statisticsInfo
	 * @return
	 */
	@AutoLog(value = "首页统计表-编辑")
	@Operation(summary="首页统计表-编辑")
	@RequiresPermissions("statisticsInfo:nm_statistics_info:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StatisticsInfo statisticsInfo) {
		statisticsInfoService.updateById(statisticsInfo);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "首页统计表-通过id删除")
	@Operation(summary="首页统计表-通过id删除")
	@RequiresPermissions("statisticsInfo:nm_statistics_info:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		statisticsInfoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "首页统计表-批量删除")
	@Operation(summary="首页统计表-批量删除")
	@RequiresPermissions("statisticsInfo:nm_statistics_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.statisticsInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "首页统计表-通过id查询")
	@Operation(summary="首页统计表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<StatisticsInfo> queryById(@RequestParam(name="id",required=true) String id) {
		StatisticsInfo statisticsInfo = statisticsInfoService.getById(id);
		if(statisticsInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(statisticsInfo);
	}
	 @Operation(summary="首页统计表-通过id查询")
	 @GetMapping(value = "/queryByCode")
	 public Result<StatisticsInfo> queryByCode() {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 StatisticsInfo statisticsInfo = statisticsInfoService.findByCode(loginUser.getOrgCode());
		 if(statisticsInfo==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(statisticsInfo);
	 }


    /**
    * 导出excel
    *
    * @param request
    * @param statisticsInfo
    */
    @RequiresPermissions("statisticsInfo:nm_statistics_info:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StatisticsInfo statisticsInfo) {
        return super.exportXls(request, statisticsInfo, StatisticsInfo.class, "首页统计表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("statisticsInfo:nm_statistics_info:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StatisticsInfo.class);
    }

}
