package com.jwipc.sys.controller.pc;

import com.alibaba.excel.EasyExcel;
import com.jwipc.page.PageView;
import com.jwipc.sys.dto.*;
import com.jwipc.sys.logic.LoginLogic;
import com.jwipc.sys.logic.SupplierChildUserInitLogic;
import com.jwipc.sys.logic.UserLogic;
import com.jwipc.sys.model.query.UserListQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhangjie
 * @since 2024-01-26
 */
@RestController
@RequestMapping(MODULE_ROOT + "/user")
@RequiredArgsConstructor
@Slf4j
public class UserRestController {
    private final UserLogic userLogic;
    private final LoginLogic loginLogic;
    private final SupplierChildUserInitLogic supplierChildUserInitLogic;

    /**
     * 列表
     *
     * @param query
     * @return
     */
    @GetMapping("list")
    public PageView list(UserListQuery query) {
        return userLogic.findByPage(query);
    }

    /**
     * 列表
     *
     * @param query 查询条件
     * @return 查询结果
     */
    @GetMapping("listAll")
    public List<UserPageListDTO> listAll(UserListQuery query) {
        return userLogic.listAll(query);
    }

    /**
     * 新增
     *
     * @return
     */
    @PostMapping("save")
    public ResponseEntity save(UserAddDTO userAddDTO) {
        userLogic.add(userAddDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新
     *
     * @return
     */
    @PostMapping("update")
    public ResponseEntity update(UserEditDTO userEditDTO) {
        userLogic.update(userEditDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新和新增一起
     *
     * @return
     */
    @PostMapping("saveOrupdate")
    public ResponseEntity saveOrupdate() {
        return null;
    }

    /**
     * 删除
     *
     * @return
     */
    @PostMapping("remove")
    public ResponseEntity remove() {
        return null;
    }

    /**
     * 导入
     *
     * @return
     */
    @PostMapping("importData")
    public ResponseEntity importData() {
        return null;
    }

    @PostMapping("updateRoleRel")
    public ResponseEntity updateRoleRel(Long userId, String roleIds) {
        userLogic.updateRoleRel(userId, roleIds);
        return ResponseEntity.ok().build();
    }

    @PostMapping("updateOrgRel")
    public ResponseEntity updateOrgRel(Long userId, String orgIds) {
        userLogic.updateOrgRel(userId, orgIds);
        return ResponseEntity.ok().build();
    }

    @PostMapping("updatePurchaseGroupRel")
    public ResponseEntity updatePurchaseGroupRel(
            @RequestBody List<UserPurchaseGroupUpdateDTO> purchaseGroupUpdateDTOS)
    {
        userLogic.updatePurchaseGroupRel(purchaseGroupUpdateDTOS);
        return ResponseEntity.ok().build();
    }

    @PostMapping("updateFactoryRel")
    public ResponseEntity updateFactoryRel(Long userId, String factoryCodes) {
        userLogic.updateFactoryRel(userId, factoryCodes);
        return ResponseEntity.ok().build();
    }

    @PostMapping("updatePurchaseOrgRel")
    public ResponseEntity updatePurchaseOrgRel(Long userId, String purchaseOrgCodes) {
        userLogic.updatePurchaseOrgRel(userId, purchaseOrgCodes);
        return ResponseEntity.ok().build();
    }

    @PostMapping("unLock")
    public ResponseEntity unLock(String accountName) {
        loginLogic.unLock(accountName);
        return ResponseEntity.ok().build();
    }

    @PostMapping("updateStatus")
    public ResponseEntity updateStatus(Long userId, Integer status) {
        userLogic.updateStatus(userId, status);
        return ResponseEntity.ok().build();
    }

    @PostMapping("restPassword")
    public ResponseEntity restPassword(Long userId) {
        userLogic.restPassword(userId);
        return ResponseEntity.ok().build();
    }

    @PostMapping("updatePassword")
    public ResponseEntity updatePassword(UpdatePasswordDTO updatePasswordDTO) {
        userLogic.updatePassword(updatePasswordDTO);
        return ResponseEntity.ok().build();
    }
    @PostMapping("updatePasswordFirstLogin")
    public ResponseEntity updatePasswordFirstLogin(UpdatePasswordDTO updatePasswordDTO) {
        userLogic.updatePasswordFirstLogin(updatePasswordDTO);
        return ResponseEntity.ok().build();
    }

    @PostMapping("initSupplierChildUser")
    public ResponseEntity initSupplierChildUser() {
        supplierChildUserInitLogic.initSupplierChildUser();
        return ResponseEntity.ok().build();
    }

    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response, UserListQuery query) throws IOException {
        // 创建数据列表
        final List<UserPageListDTO> forExport = userLogic.findForExport(query);

        // 设置响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("用户数据", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        // 使用EasyExcel导出Excel文件
        EasyExcel.write(response.getOutputStream(), UserPageListDTO.class).sheet("Sheet1").doWrite(forExport);
    }


}
