package com.dk.mdm.controller.mst;

import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.common.util.ExcelUtils;
import com.dk.mdm.model.pojo.mst.Staff;
import com.dk.common.service.BaseService;
import com.dk.mdm.model.query.mst.StaffQuery;
import com.dk.common.model.response.mst.StaffResponse;
import com.dk.common.model.vo.mst.StaffVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.dk.mdm.service.mst.StaffService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import java.util.Map;

/**
 * @author : 姜永辉
 * @desc : 员工API接口
 * @date : 2023/1/4 9:25
 */
@Api(tags = "员工API接口")
@RestController
@RequestMapping("/mst/staff")
@Slf4j
public class StaffController {

    public BaseService<Staff> getService() {
        return staffService;
    }

    @Autowired
    private StaffService staffService;


    /**
     * @desc : 条件查询
     * @author : 姜永辉
     * @date : 2023/1/9 10:36
     */
    @ApiOperation(value = "分页、关联、条件查询", notes = "分页、关联、条件查询")
    @PostMapping({"list_by"})
    public ResponseResultVO<PageList<StaffResponse>> selectByCond(@RequestBody StaffQuery staffQuery) {
        return staffService.selectByCond(staffQuery);
    }

    /**
     * @desc : 通过ID查询
     * @author : 姜永辉
     * @date : 2023/1/9 10:41
     */
    @PostMapping({"/{id}"})
    public ResponseResultVO selectById(@PathVariable String id) {
        return staffService.selectById(id);
    }

    /**
     * @desc : 新建员工--权限
     * @author : 姜永辉
     * @date : 2023/1/9 10:48
     */
    @ApiOperation(value = "新建员工--权限", notes = "新建员工--权限")
    @PostMapping({"save_staff_right"})
    public ResponseResultVO<?> saveStaffRight(@RequestBody StaffVO staffVO) {
        return staffService.saveStaffRight(staffVO);
    }

    /**
     * @desc : 新建员工--权限
     * @author : 姜永辉
     * @date : 2023/1/9 10:48
     */
    @ApiOperation(value = "新建员工--权限", notes = "新建员工--权限")
    @PostMapping({"save_staff_preview"})
    public ResponseResultVO<?> saveStaffPurview(@RequestBody StaffVO staffVO) {
        return staffService.saveStaffPurview(staffVO);
    }


    /**
     * @desc : 新建员工
     * @author : 姜永辉
     * @date : 2023/1/9 10:48
     */
    @ApiOperation(value = "新建员工", notes = "新建员工")
    @PostMapping({"insert"})
    public ResponseResultVO<?> insert(@RequestBody StaffVO staffVO) {
        return staffService.insert(staffVO);
    }

    /**
     * @desc : 新建员工--邀请员工的确定
     * @author : 姜永辉
     * @date : 2023/1/9 10:48
     */
    @ApiOperation(value = "邀请员工的确定", notes = "邀请员工的确定")
    @PostMapping({"insert_request"})
    public ResponseResultVO<?> insertRequestStaff(@RequestBody StaffVO staffVO) {
        return staffService.insertRequestStaff(staffVO);
    }

    /**
     * @desc : 新建员工--注册商户用的
     * @author : 姜永辉
     * @date : 2023/1/9 10:48
     */
    @ApiOperation(value = "新建员工", notes = "新建员工")
    @PostMapping({"insert_feign_staff"})
    public ResponseResultVO<?> insertFeignStaff(@RequestBody StaffVO staffVO) {
        return staffService.insertFeignStaff(staffVO);
    }

    /**
     * @desc : 注册商户时候插入  角色 基础资料（仓库 渠道 收入 支出 入库 出库 账户 来源 盈亏原因） 计量单位 种类  打印票据  等等。
     *         返回角色的数据
     * @author : 姜永辉
     * @date : 2023/1/9 10:48
     */
    @ApiOperation(value = "注册商户时候插入基础信息", notes = "注册商户时候插入基础信息")
    @PostMapping({"insert_feign_company_data"})
    public ResponseResultVO<?> insertFeignCompanyData(@RequestBody Map<String, List<Map<String, Object>>> map) {
        return staffService.insertFeignCompanyData(map);
    }

    /**
     * @desc : 编辑员工
     * @author : 姜永辉
     * @date : 2023/1/9 10:49
     */
    @ApiOperation(value = "编辑员工", notes = "编辑员工")
    @PostMapping({"update"})
    public ResponseResultVO<Boolean> update(@RequestBody StaffVO staffVO) {
        return staffService.update(staffVO);
    }

    /**
     * @desc : 停用
     * @author : 姜永辉
     * @date : 2023/1/9 10:34
     */
    @ApiOperation(value = "停用", notes = "停用")
    @PostMapping("disable/{id}")
    public ResponseResultVO<Boolean> disable(@PathVariable String id) {
        return this.getService().disable(id);
    }

    /**
     * @desc : 启用
     * @author : 姜永辉
     * @date : 2023/1/9 10:34
     */
    @ApiOperation(value = "启用", notes = "启用")
    @PostMapping("enable/{id}")
    public ResponseResultVO<Boolean> enable(@PathVariable String id) {
        return this.getService().enable(id);
    }

    /**
     * @desc : 员工离职
     * @author : 姜永辉
     * @date : 2023/2/13 13:15
     */
    @ApiOperation(value = "员工离职", notes = "员工离职")
    @PostMapping({"dimission"})
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultVO<Boolean> dimission(@RequestBody StaffVO staffVO) {
        return staffService.dimission(staffVO);
    }

    /**
     * @desc : 员工导入
     * @author : 姜永辉
     * @date : 2023/2/28 14:32
     */
    @ApiOperation(value = "导入", notes = "导入")
    @PostMapping("import")
    @Transactional(rollbackFor = Exception.class)
    public ResponseResultVO<Boolean> importList(MultipartFile file) {
        List<StaffVO> list = ExcelUtils.importExcel(file, 1, 1, StaffVO.class);
        return staffService.importStaffList(list);
    }

    /**
     * @desc : 导出模板
     * @author : 洪旭东
     * @date : 2023-08-01 11:44
     */
    @ApiOperation(value = "导出模板", notes = "导出模板")
    @GetMapping("export_template")
    public void exportTemplate(HttpServletResponse response) {
        String title = "员工导入模板";
        ExcelUtils.exportExcel(new ArrayList<>(), title, title, StaffVO.class, title + ".xls", response);
    }


    /**
     * @desc : 体验产品-从oauth服务调用的函数
     * @author : 姜永辉
     * @date : 2023-11-02 16:27
     */
    @PostMapping({"/feign_get_experience"})
    ResponseResultVO<StaffResponse> getFeignExperience(@RequestBody Map<String, Object> map) {
        log.info("feign_get_experience111111111" + map.toString());
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setCpId(Integer.parseInt(map.get("cpId").toString()));
        staffQuery.setStaffCode(map.get("staffCode").toString());
        ResponseResultVO<PageList<StaffResponse>> pageListResponseResultVO = staffService.selectByCond(staffQuery);
        log.info("feign_get_experience2" + pageListResponseResultVO.toString());
        return ResponseResultUtil.success(pageListResponseResultVO.getData().getList().get(0));
    }

    /**
     * @desc : 登录后获取信息
     * @author : 周兴
     * @date : 2024/3/4 12:47
     */
    @ApiOperation(
            value = "登录后获取信息",
            notes = "登录后获取信息"
    )
    @PostMapping("get_info_after_login")
    public ResponseResultVO<Dictionary<String, Object>> getInfoAfterLogin(@RequestBody Map<String, Object> param) {
        return staffService.getInfoAfterLogin(param);
    }

    /**
     * @desc : 退出登录
     * @author : 周兴
     * @date : 2024/3/18 12:47
     */
    @ApiOperation(
            value = "退出登录",
            notes = "退出登录"
    )
    @PostMapping("logout")
    public ResponseResultVO<Boolean> logout(@RequestBody Map<String, Object> param) {
        return staffService.logout(param);
    }

}
