package com.cpop.mall.business.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.cpop.common.constant.Constants;
import com.cpop.core.base.R;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.mall.business.bo.ModifyUserPasswordBo;
import com.cpop.mall.business.bo.StaffBo;
import com.cpop.mall.business.bo.StaffPageBo;
import com.cpop.mall.business.entity.RoleBrand;
import com.cpop.mall.business.service.RoleBrandService;
import com.cpop.mall.business.vo.BrandListVo;
import com.cpop.mall.business.vo.StaffInfoVo;
import com.cpop.mall.business.vo.StaffPageVo;
import com.cpop.system.business.service.BrandService;
import com.cpop.system.business.vo.RoleVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.cpop.mall.business.service.StaffService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;

import static com.cpop.mall.business.entity.table.RoleBrandTableDef.ROLE_BRAND;
import static com.cpop.system.business.entity.table.BrandTableDef.BRAND;
import static com.cpop.system.business.entity.table.RoleTableDef.ROLE;

/**
 * 员工表 控制层。
 *
 * @author DB
 * @since 2023-10-19
 */
@RestController
@Api(tags = "商城员工接口")
@RequestMapping("/backstage/mallStaff")
public class BackstageStaffController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private BrandService brandService;

    /**
     * @descriptions 查询员工分页列表
     * @author DB
     * @date 2023/09/07 18:07
     * @param bo 请求参数
     * @return R<Page<SysStaffPageVo>>
     */
    @PreAuthorize("@aps.hasPermission('system:account:list')")
    @ApiOperation("查询员工分页列表")
    @GetMapping("/getStaffPageList")
    public R<Page<StaffPageVo>> getStaffPageList(StaffPageBo bo) {
        Page<StaffPageVo> pageVo = staffService.getStaffPageList(bo);
        return R.ok(pageVo);
    }

    /**
     * @descriptions 新增员工
     * @author DB
     * @date 2023/09/08 14:04
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('system:account:insert')")
    @ApiOperation("新增员工")
    @PostMapping("/insertStaff")
    public R<Void> insertStaff(@RequestBody @Validated StaffBo bo) {
        staffService.insertStaff(bo);
        return R.ok();
    }

    /**
     * @descriptions 修改员工
     * @author DB
     * @date 2023/09/08 15:20
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('system:account:update')")
    @ApiOperation("修改员工")
    @PutMapping("/updateStaff")
    public R<Void> updateStaff(@RequestBody @Validated StaffBo bo) {
        staffService.updateStaff(bo);
        return R.ok();
    }

    /**
     * @descriptions 删除员工
     * @author DB
     * @date 2023/09/12 16:57
     * @param id 主键
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('system:account:remove')")
    @ApiOperation("删除员工")
    @DeleteMapping("/removeStaffById/{id}")
    public R<Void> removeStaffById(@PathVariable String id) {
        staffService.removeStaffById(id);
        return R.ok();
    }

    /**
     * 获取所有角色信息
     */
    @ApiOperation("获取所有角色信息")
    @GetMapping("/getAllRoleList")
    public R<List<RoleVo>> getAllSysRoleList() {
        //获取当前登陆用户所在品牌
        JSONObject loginStaffInfo = SecurityUtils.getInstance().getLoginUserInfo();
        String roleBrandId = loginStaffInfo.getString("roleBrandId");
        RoleBrandService roleBrandService = SpringUtils.getBean(RoleBrandService.class);
        RoleBrand roleBrand = roleBrandService.getById(roleBrandId);
        List<RoleVo> list = roleBrandService.listAs(QueryWrapper.create()
                        .from(ROLE_BRAND)
                        .leftJoin(ROLE).on(ROLE.ID.eq(ROLE_BRAND.ROLE_ID))
                        .where(ROLE.STATUS.eq(true))
                        .and(ROLE_BRAND.BRAND_ID.eq(roleBrand.getBrandId()))
                        .and(ROLE.ROLE_VALUE.ne(Constants.SUPER_ADMIN_VALUE))
                        .orderBy(ROLE.ORDER_NO.asc())
                , RoleVo.class);
        return R.ok(list);
    }

    /**
     * @Description: 用户名是否存在
     * @param username 用户名
     * @param id 主键
     * @return: R<Void>
     * @Author: DB
     * @Date: 2023/5/11 10:09
     **/
    @ApiOperation("用户名是否存在")
    @GetMapping("/isAccountExist")
    public R<Void> isAccountExist(@ApiParam("用户名") String username, @ApiParam("userId") String id) {
        staffService.isAccountExist(username, id);
        return R.ok();
    }

    /**
     * @Description: 获取员工信息
     * @param id 主键
     * @return: R<StaffVo>
     * @Author: DB
     * @Date: 2023/5/17 10:33
     **/
    @PreAuthorize("@aps.hasPermission('system:account:info')")
    @ApiOperation("获取员工信息")
    @GetMapping("/getStaffInfo/{id}")
    public R<StaffInfoVo> getStaffInfo(@PathVariable String id) {
        StaffInfoVo staffInfo = staffService.getStaffInfo(id);
        return R.ok(staffInfo);
    }

    /**
     * @Description: 修改系统用户密码
     * @param bo 请求参数
     * @return: AjaxResult<Void>
     * @Author: DB
     * @Date: 2023/5/12 16:00
     **/
    @PreAuthorize("@aps.hasPermission('system:account:update')")
    @ApiOperation("修改系统用户密码")
    @PutMapping("/modifyUserPassword")
    public R<Void> modifyUserPassword(@Validated @RequestBody ModifyUserPasswordBo bo) {
        staffService.modifyUserPassword(bo);
        return R.ok();
    }

    /**
     * @descriptions 查询员工分页列表
     * @author DB
     * @date 2023/09/07 18:07
     * @return R<Page<SysStaffPageVo>>
     */
    @ApiOperation("查询品牌列表")
    @GetMapping("/getBrandList")
    public R<List<BrandListVo>> getBrandList(String query) {
        List<BrandListVo> list = brandService.listAs(QueryWrapper.create().and(BRAND.BRAND_NAME.like(query)), BrandListVo.class);
        return R.ok(list);
    }

}
