package io.renren.modules.business.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.business.entity.*;
import io.renren.modules.business.service.*;
import io.renren.modules.business.service.BusinessDictService;
import io.renren.modules.business.service.BusinessWorkerCqService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lsr
 * @version 2022/10/16
 * 支出明细
 */
@RestController
@RequestMapping("business/businesszcmx")
public class BusinessZcmxController {

    @Autowired
    private BusinessOutlayService businessOutlayService;
    @Autowired
    private BusinessDictService businessDictService;

    @Autowired
    private BusinessOutlayIncomeService businessOutlayIncomeService;

    @Autowired
    private BusinessWorkerCqService businessWorkerCqService;
    @Autowired
    private BusinessProjectService businessProjectService;
    @Autowired
    private BusinessWorkerService businessWorkerService;
    @Autowired
    private BusinessStService businessStService;

    /**
     * 获取字典信息
     */
    @RequestMapping("/dictList")
    @RequiresPermissions("business:businesszcmx:list")
    public R dictList(@RequestParam Map<String, Object> params){
        // PageUtils page = businessDictService.queryPage(params);
        List<BusinessDictEntity> businessDictEntity = businessDictService.listWithTree();
        return R.ok().put("data", businessDictEntity);
    }

    /**
     * 获取所有的立项列表未删除的
     */
    @RequestMapping("/projectAllList")
    @RequiresPermissions("business:businesszcmx:list")
    public R projectAllList(){
        //System.out.println(params);
        List<BusinessProjectEntity> page = businessProjectService.allList();
        return R.ok().put("page", page);
    }


    /**
     * 员工 包含离职列表
     */
    @RequestMapping("/selectAllContainsDeparturePage")
    @RequiresPermissions("business:businesszcmx:list")
    public R selectAllContainsDeparturePage(@RequestParam Map<String, Object> params) {
        PageUtils page = businessWorkerService.selectAllContainsDeparturePage(params);
        return R.ok().put("page", page);
    }

    /**
     * 员工 包含离职列表
     */
    @RequestMapping("/selectAllContainsDeparture")
    @RequiresPermissions("business:businesszcmx:list")
    public R selectAllContainsDeparture(@RequestParam Map<String, Object> params) {
        List<BusinessWorkerEntity> page = businessWorkerService.selectAllContainsDeparture(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取支出最大时间列表
     */
    @RequestMapping("/biggerTimeList")
    @RequiresPermissions("business:businesszcmx:list")
    public R biggerTimeList(){
        List<BusinessOutlayEntity> page = businessOutlayService.biggerTimeList();
        return R.ok().put("page", page);
    }

    /**
     * 获取最新的支出时间  去判断开工时间   从每个年度的2月  到第二年的三月
     * 传出  值为每个年度的开工时间
     */
    @RequestMapping("/getLatestTime")
    @RequiresPermissions("business:businesszcmx:list")
    public R getLatestTime() throws ParseException {
        Map<String, Object> page = businessOutlayService.getLatestTime();
        return R.ok().put("page", page);
    }

    /**
     * 列表-支出查询分页
     */
    @RequestMapping("/zccxList")
    @RequiresPermissions("business:businesszcmx:list")
    public R zccxList(@RequestParam Map<String, Object> params){
        PageUtils page = businessOutlayService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 列表-支出查询合计
     */
    @RequestMapping("/zccxhj")
    @RequiresPermissions("business:businesszcmx:list")
    public R zccxhj(@RequestParam Map<String, Object> params){
        Map<String, Object> page = businessOutlayService.getZccxhj(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表-支出查询
     */
    @RequestMapping("/zcList")
    @RequiresPermissions("business:businesszcmx:list")
    public R list(@RequestParam Map<String, Object> params){
        //System.out.println(params);
        Map<String,Object> page = businessOutlayService.queryList(params);
        return R.ok().put("page", page);
    }


    /**
     * 获取执行人列表
     */
    @GetMapping("/getZxr")
    public R getZxr(){
        //System.out.println(params);
        List<Map<String,Object>> page = businessOutlayService.getZxrList();
        return R.ok().put("page", page);
    }
    /**
     * 获取类别统计信息
     * @param params
     * @return
     */
    @RequestMapping("/zclbList")
    @RequiresPermissions("business:businesszcmx:list")
    public R zclbList(@RequestParam Map<String, Object> params){
        //System.out.println(params);
        Map<String,Object> page = businessOutlayIncomeService.zclbList(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取四人类别统计信息
     * @param params
     * @return
     */
    @RequestMapping("/srlbList")
//    @RequiresPermissions("business:businesszcmx:list")
    public R srlbList(@RequestParam Map<String, Object> params){
        //System.out.println(params);
        Map<String,Object> page = businessOutlayService.srTypeList(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取项目成本
     * @param params
     * @return
     */
    @RequestMapping("/getXmcb")
//    @RequiresPermissions("business:businesszcmx:list")
    public R getXmcb(@RequestParam Map<String, Object> params){
        //System.out.println(params);
        Map<String,Object> data = businessOutlayService.getXmcb(params);
        return  R.ok().put("page", data);
    }

    /**
     * 获取工资统计
     * @return
     */
    @GetMapping("/getGzTj")
    public R getGzTj(@RequestParam Map<String,Object> params){
        //System.out.println(params);
        Map<String,Object> data = businessOutlayService.getGzTj(params);
        return  R.ok().put("page", data);
    }

    /**
     * 获取管理费
     * @return
     */
    @GetMapping("/getGlf")
    public R getGlf(@RequestParam Map<String,Object> params){
        Map<String,Object> data = businessOutlayService.getGlf(params);
        return  R.ok().put("page", data);
    }

    /**
     * 获取出勤记录信息
     * @param params
     * @return
     */
    @RequestMapping("/cqjlList")
    @RequiresPermissions("business:businesszcmx:list")
    public R cqjlList(@RequestParam Map<String, Object> params){
        //System.out.println(params);
//        List<BusinessWorkerCqEntity> page = businessWorkerCqService.cqjlList(params);
        PageUtils page = businessWorkerCqService.cqjlPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取食堂标准信息
     */
    @RequestMapping("/stbzList")
    @RequiresPermissions("business:businesszcmx:list")
    public R stbzList(@RequestParam Map<String, Object> params){
        //System.out.println(params);
        Map<String,Object> page = businessOutlayService.stbzList(params);
        return R.ok().put("page", page);
    }

    /**
     * 修改
     */
    @RequestMapping("/updateStxzList")
    @RequiresPermissions("business:businesszcmx:list")
    public R updateStxzList(@RequestBody Map<String, Object> params) throws ParseException {
        List<Map<String,Object>> xzList = (List<Map<String,Object>>)params.get("list");
        List<BusinessStEntity> businessStEntityList = new ArrayList<>();
        for (Map<String, Object> item : xzList) {
            BusinessStEntity businessStEntity = new BusinessStEntity();
            if (item.get("id") != null && !item.get("id").toString().equals("")) {
                businessStEntity.setId(Integer.parseInt(item.get("id").toString()));
            }
            if (item.get("dateA") != null && !item.get("dateA").toString().equals("")) {
                String dateDayStr = item.get("dateA").toString() + "-01";
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = dateFormat.parse(dateDayStr);
                businessStEntity.setDateDay(date);
            }
            if (item.get("stday") != null && !item.get("stday").toString().equals("")) {
                businessStEntity.setFixDay(new BigDecimal(item.get("stday").toString()));
            }
            businessStEntityList.add(businessStEntity);
        }
        businessStService.saveOrUpdateBatch(businessStEntityList);
        return R.ok();
    }

    /**
     * 获取转账验证数据
     */
    @RequestMapping("/yzList")
    @RequiresPermissions("business:businesszcmx:list")
    public R yzList(@RequestParam Map<String, Object> params){
        List<Map<String,Object>> page = businessOutlayService.yzList(params);
        return R.ok().put("page", page);
    }

}
