package com.yanzhu.manage.controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.common.core.utils.poi.ExcelUtil;
import com.yanzhu.common.core.web.controller.BaseController;
import com.yanzhu.common.core.web.domain.AjaxResult;
import com.yanzhu.common.core.web.page.TableDataInfo;
import com.yanzhu.common.log.annotation.Log;
import com.yanzhu.common.log.enums.BusinessType;
import com.yanzhu.common.security.annotation.RequiresPermissions;
import com.yanzhu.manage.domain.ProProjectInfoSubdeptsUsers;
import com.yanzhu.manage.service.IProProjectInfoSubdeptsUsersService;
import com.yanzhu.manage.service.IUniService;
import com.yanzhu.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 分包单位工人Controller
 * 
 * @author JiangYuQi
 * @date 2024-08-25
 */
@RestController
@RequestMapping("/proProjectInfoSubdeptsUsers")
public class ProProjectInfoSubdeptsUsersController extends BaseController
{
    @Autowired
    private IProProjectInfoSubdeptsUsersService proProjectInfoSubdeptsUsersService;

    @Autowired
    private IUniService uniService;
    /**
     * 查询分包单位工人列表
     */
    @RequiresPermissions("manage:proProjectInfoSubdeptsUsers:list")
    @GetMapping("/list")
    public TableDataInfo list(ProProjectInfoSubdeptsUsers proProjectInfoSubdeptsUsers)
    {
        startPage();
        List<ProProjectInfoSubdeptsUsers> list = proProjectInfoSubdeptsUsersService.selectProProjectInfoSubdeptsUsersList(proProjectInfoSubdeptsUsers);
        return getDataTable(list);
    }

    /**
     * 导出分包单位工人列表
     */
    @RequiresPermissions("manage:proProjectInfoSubdeptsUsers:export")
    @Log(title = "分包单位工人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProProjectInfoSubdeptsUsers proProjectInfoSubdeptsUsers)
    {
        List<ProProjectInfoSubdeptsUsers> list = proProjectInfoSubdeptsUsersService.selectProProjectInfoSubdeptsUsersList(proProjectInfoSubdeptsUsers);
        for(ProProjectInfoSubdeptsUsers u :list){
            SysUser su=u.getUser();
            u.setUserName(su.getUserName());
            u.setCardCode(su.getCardCode());
            u.setUserSex(su.getSex());
            u.setEnterState("0".equals(u.getEnterState())?"在场":"离场");
            JSONObject jo=JSONObject.parseObject(su.getUserInfos());
            u.setUserNation(jo.getString("nation"));
            Date dt=jo.getDate("birthDay");
            if(dt!=null){
               int age=(int)((new Date().getTime()-dt.getTime())/1000/3600/24/365);
               u.setUserAge(age);
            }
        }
        ExcelUtil<ProProjectInfoSubdeptsUsers> util = new ExcelUtil<ProProjectInfoSubdeptsUsers>(ProProjectInfoSubdeptsUsers.class);
        util.exportExcel(response, list, "分包单位工人数据");
    }

    /**
     * 获取分包单位工人详细信息
     */
    @RequiresPermissions("manage:proProjectInfoSubdeptsUsers:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(proProjectInfoSubdeptsUsersService.selectProProjectInfoSubdeptsUsersById(id));
    }

    /**
     * 新增分包单位工人
     */
    @RequiresPermissions("manage:proProjectInfoSubdeptsUsers:add")
    @Log(title = "分包单位工人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProProjectInfoSubdeptsUsers ppUser)
    {
        return toAjax(proProjectInfoSubdeptsUsersService.insertProProjectInfoSubdeptsUsers(ppUser));
    }

    /**
     * 修改分包单位工人
     */
    @RequiresPermissions("manage:proProjectInfoSubdeptsUsers:edit")
    @Log(title = "分包单位工人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProProjectInfoSubdeptsUsers proProjectInfoSubdeptsUsers)
    {
        return toAjax(proProjectInfoSubdeptsUsersService.updateProProjectInfoSubdeptsUsers(proProjectInfoSubdeptsUsers));
    }

    /**
     * 删除分包单位工人
     */
    @RequiresPermissions("manage:proProjectInfoSubdeptsUsers:remove")
    @Log(title = "分包单位工人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(proProjectInfoSubdeptsUsersService.deleteProProjectInfoSubdeptsUsersByIds(ids));
    }
    @RequiresPermissions("manage:proProjectInfoSubdeptsUsers:remove")
    @PostMapping("/enterState/{state}")
    public AjaxResult updateEnterState(@RequestBody  List<Long> ids,@PathVariable int state){
        try {
            for (Long id : ids) {
                ProProjectInfoSubdeptsUsers user=proProjectInfoSubdeptsUsersService.selectProProjectInfoSubdeptsUsersById(id);
                if(user!=null) {
                    if (state == 0) {//进场
                        uniService.syncUserAuthDevice(id);
                    } else {
                        uniService.syncUserRevoke(id);
                    }
                    user.setEnterState(""+state);
                    proProjectInfoSubdeptsUsersService.updateProProjectInfoSubdeptsUsers(user);
                }
            }
            return AjaxResult.success();
        }catch (Exception ex){
            return  AjaxResult.error(ex.getMessage());
        }
    }
}
