package io.renren.modules.business.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.business.entity.BusinessWorkerCqEntity;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 员工出勤表
 *
 * @author jiaqiao
 * @email jiaqiao@163.com
 * @date 2022-09-20 11:26:31
 */
@RestController
@RequestMapping("business/businessworkercq")
public class BusinessWorkerCqController {
    @Autowired
    private BusinessWorkerCqService businessWorkerCqService;

    /**
     * 列表
     */
    /*@RequestMapping("/list")
    @RequiresPermissions("business:businessworkercq:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessWorkerCqService.queryPage(params);

        return R.ok().put("page", page);
    }*/
    @RequestMapping("/list")
    @RequiresPermissions("business:businessworkercq:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessWorkerCqService.queryPage(params);
//        List<BusinessWorkerCqEntity> page = businessWorkerCqService.querySearchList(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/listAll")
    @RequiresPermissions("business:businessworkercq:list")
    public R listAll(@RequestParam Map<String, Object> params){
        List<Map<String, Object>> page = businessWorkerCqService.querySearchListAll(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:businessworkercq:info")
    public R info(@PathVariable("id") Integer id){
		BusinessWorkerCqEntity businessWorkerCq = businessWorkerCqService.getById(id);

        return R.ok().put("businessWorkerCq", businessWorkerCq);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businessworkercq:save")
    public R save(@RequestBody BusinessWorkerCqEntity businessWorkerCq){
        if(businessWorkerCq.getId() != null && businessWorkerCq.getId() != 0) {
            businessWorkerCqService.updateById(businessWorkerCq);
        } else {
            businessWorkerCqService.save(businessWorkerCq);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businessworkercq:update")
    public R update(@RequestBody BusinessWorkerCqEntity businessWorkerCq){
		businessWorkerCqService.updateById(businessWorkerCq);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updateList")
    @RequiresPermissions("business:businessworkercq:update")
    public R updateList(@RequestBody Map<String, Object> params) throws ParseException {
        String dateDay = params.get("dateDay").toString();
        List<Map<String,Object>> businessWorkerCqEntityList = (List<Map<String,Object>>)params.get("list");
        List<BusinessWorkerCqEntity> businessWorkerCqUpdateEntityList = new ArrayList<>();
        for (Map<String, Object> item : businessWorkerCqEntityList) {
            BusinessWorkerCqEntity businessWorkerCqEntity = new BusinessWorkerCqEntity();
            if (item.get("id") != null && !item.get("id").toString().equals("")) {
                businessWorkerCqEntity.setId(Integer.parseInt(item.get("id").toString()));
            }
            if (item.get("userId") != null && !item.get("userId").toString().equals("")) {
                businessWorkerCqEntity.setUserId(Integer.parseInt(item.get("userId").toString()));
            }
            if (item.get("fixDay")!=null && !"".equals(item.get("fixDay").toString().trim())) {
                businessWorkerCqEntity.setFixDay(item.get("fixDay").toString());
            }
            if (!Objects.isNull(item.get("remark"))) {
                businessWorkerCqEntity.setRemark(item.get("remark").toString());
            }
            if (!dateDay.equals("")) {
                String dateDayStr = dateDay + "-01";
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = dateFormat.parse(dateDayStr);
                businessWorkerCqEntity.setDateDay(date);
            }
            businessWorkerCqEntity.setApproval(0);
            businessWorkerCqUpdateEntityList.add(businessWorkerCqEntity);
        }
        businessWorkerCqService.saveOrUpdateBatch(businessWorkerCqUpdateEntityList);

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:businessworkercq:update")
    public R delete(@RequestBody Integer[] ids){
		businessWorkerCqService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
