package org.dromara.payment.worker.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.payment.common.domain.CommonSelConditionEntity;
import org.dromara.payment.worker.domain.UserWorkerBusiness;
import org.dromara.payment.worker.domain.bo.UserWokerOperBo;
import org.dromara.payment.worker.domain.bo.UserWorkerAuthorizedBo;
import org.dromara.payment.worker.domain.bo.UserWorkerBo;
import org.dromara.payment.worker.domain.vo.*;
import org.dromara.payment.worker.service.IUserWorkerAuthorizedService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.payment.worker.domain.bo.UserWorkerBusinessBo;
import org.dromara.payment.worker.service.IUserWorkerBusinessService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 用工签约查询
 * 前端访问路由地址为:/worker/workerBusiness
 *
 * @author LionLi
 * @date 2024-04-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workerBusiness")
public class
UserWorkerBusinessController extends BaseController {

    private final IUserWorkerBusinessService userWorkerBusinessService;


    @Resource
    private IUserWorkerAuthorizedService authorizedService;


    /**
     * 查询用工签约查询列表
     */
//    @SaCheckPermission("worker:workerBusiness:list")
    @GetMapping("/operlist")
    @Log(title = "运营商添加用工签约列表查询-运营商", businessType = BusinessType.SELECT)
    public TableDataInfo<UserWorkerBusinessVo> list( PageQuery pageQuery,Long merId,
                                                     Long sydId,Integer busType) {
        return userWorkerBusinessService.selUserWorkByMerServInfo(pageQuery,sydId,merId,busType);
    }

    /**
     * 查询用工签约查询列表
     */
//    @SaCheckPermission("worker:workerBusiness:list")
    @GetMapping("/list")
    public TableDataInfo<UserWorkerBusinessVo> list(CommonSelConditionEntity bo, PageQuery pageQuery) {
        return userWorkerBusinessService.queryPageList(bo, pageQuery);
    }


    /***
     * 授权协议分页查询
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/authList")
    public TableDataInfo<UserWorkerAuthorizedVo> list(UserWorkerAuthorizedBo bo, PageQuery pageQuery) {
        return this.authorizedService.queryPageList(bo, pageQuery);
    }


    /**
     * 导出用工签约查询列表
     */
//    @SaCheckPermission("worker:workerBusiness:export")
    @Log(title = "用工签约查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommonSelConditionEntity bo, HttpServletResponse response) {
        List<UserWorkerBusinessVo> list = userWorkerBusinessService.queryList(bo);
        ExcelUtil.exportExcel(list, "用工签约导出", UserWorkerBusinessVo.class, response);
    }


    @Log(title = "单个导出用工的身份证信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCardImg")
    public void exportCardImg(Long workId,HttpServletResponse response)throws Exception{
        this.userWorkerBusinessService.exportCardImg(workId,response);
    }


    @Log(title = "批量导出用工的身份证信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportMutiCardImg")
    public R<Boolean> exportMutiCardImgs(String workIds)throws Exception{
        this.userWorkerBusinessService.exportMutiCardImg(workIds);
        return R.ok(true);
    }


    /**
     * 获取用工签约查询详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("worker:workerBusiness:query")
    @GetMapping("/{id}")
    public R<UserWorkerBusinessVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userWorkerBusinessService.queryById(id));
    }



    /**
     * 获取用工签约查询详细信息 -授权协议
     *
     * @param id 主键
     */
//    @SaCheckPermission("worker:workerBusiness:getAuth")s
    @GetMapping("/getAuth")
    public R<UserWorkerAuthorizedVo> getAuthInfo(@NotNull(message = "主键不能为空")
                                            Long id) {
        return R.ok(this.authorizedService.queryById(id));
    }

    /**
     * 新增用工签约查询
     */
    @SaCheckPermission("worker:workerBusiness:add")
    @Log(title = "用工签约查询", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserWorkerBusinessBo bo) {
        return toAjax(userWorkerBusinessService.insertByBo(bo));
    }

    /**
     * 修改用工签约查询
     */
    @SaCheckPermission("worker:workerBusiness:edit")
    @Log(title = "用工签约查询", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserWorkerBusinessBo bo) {
        return toAjax(userWorkerBusinessService.updateByBo(bo));
    }

    /**
     * 删除用工签约查询
     *
     * @param ids 主键串
     */
    @SaCheckPermission("worker:workerBusiness:remove")
    @Log(title = "用工签约查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userWorkerBusinessService.deleteWithValidByIds(List.of(ids), true));
    }





//    @SaCheckPermission("worker:workerBusiness:import")
    @Log(title = "用工签约导入", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<ImportResultVo> upload(@RequestPart("file") MultipartFile file,
                                    Integer busType,Long sydId ,String sydNo,String domain,Long merId,String merNo )throws Exception{
        return R.ok(this.userWorkerBusinessService.upload(file,busType,sydId,sydNo,domain,merId,merNo));
    }



    /**
     * 导出用工签约查询列表
     */
//    @SaCheckPermission("worker:workerBusiness_error:export")
    @Log(title = "用工签约错误信息下载", businessType = BusinessType.EXPORT)
    @PostMapping("/exportError")
    public void exportSignError(String uuid, HttpServletResponse response) {
        List<UploadExcelItem> list = userWorkerBusinessService.selErrorList(uuid);
        ExcelUtil.exportExcel(list, "用工签约导入错误信息", UploadExcelItem.class, response);
    }



    /**
     * 用工签约，查询会员信息
     */
//    @SaCheckPermission("worker:workerBusiness:selMember")
    @Log(title = "用工签约查询会员信息", businessType = BusinessType.OTHER)
    @PostMapping("/selWkInfo")
    public R<UserWorkerVo> selWkInfo(UserWorkerBusinessBo bo) {
        UserWorkerVo vo = userWorkerBusinessService.selMemberInfo(bo);
        return R.ok(vo);
    }



    /***
     * 添加用工人员-运营商
     * @param bo
     * @return
     */
    @Log(title = "运营商添加用工人员", businessType = BusinessType.OTHER)
    @PostMapping("/addUwb")
    public R<Boolean> addUserWorkdBz(@Validated(AddGroup.class) @RequestBody UserWokerOperBo bo)throws Exception{
        return R.ok(userWorkerBusinessService.addUserWorkdBz(bo));
    }



    /***
     * 用工签约- -运营商
     * @return
     */
    @Log(title = "用工签约-选中提交 -运营商", businessType = BusinessType.INSERT)
    @PostMapping("/addSelUwb")
    public R<Boolean> operAddUserWorkSel(Long merId,Long sydId,Integer busType,String workIds){
        return R.ok(this.userWorkerBusinessService.operAddUserWorkSel(merId,sydId,busType,workIds));
    }


    /***
     * 用工签约- -运营商
     * @return
     */
    @Log(title = "用工签约-全部提交 -运营商", businessType = BusinessType.INSERT)
    @PostMapping("/addAllUwb")
    public R<Boolean> operAddUserWorkAll(Long merId,Long sydId,Integer busType){
        return R.ok(this.userWorkerBusinessService.operAddUserWorkAll(merId,sydId,busType));
    }



    @Log(title = "用工信息修改-运营商", businessType = BusinessType.UPDATE)
    @PostMapping("/upUw")
    public R<Boolean> updateUserWorkInfo(UserWorkerBo uw){
        return R.ok(this.userWorkerBusinessService.updateUserWorkInfo(uw));
    }



    @Log(title = "用工信息修改查询-运营商", businessType = BusinessType.UPDATE)
    @PostMapping("/selUpUw")
    public R<UserWorkerNoHideVo> updateUserWorkInfo(UserWorkerBusinessBo uw){
        return R.ok(this.userWorkerBusinessService.selMemberInfoNySignId(uw));
    }


}
