package com.starry.admin.modules.clerk.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserQueryVo;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserReturnVo;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserTypeEditVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkUserEditVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkUserStateEditVo;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity;
import com.starry.admin.modules.custom.service.IPlayCustomUserInfoService;
import com.starry.admin.modules.weichat.entity.clerk.PlayClerkUserInfoQueryVo;
import com.starry.admin.modules.weichat.entity.clerk.PlayClerkUserInfoResultVo;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 店员Controller
 *
 * @author admin
 * @since 2024-03-30
 */
@RestController
@RequestMapping("/clerk/user")
public class PlayClerkUserInfoController {

    @Resource
    private IPlayCustomUserInfoService customUserInfoService;
    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;

    /**
     * 查询店员列表
     */
    @PostMapping("listByPage")
    public R listByPage(@Validated @RequestBody PlayClerkUserQueryVo vo) {
        IPage<PlayClerkUserReturnVo> list = playClerkUserInfoService.selectByPage(vo);
        return R.ok(list);
    }

    /**
     * 按照店员分类查询店员列表
     */
    @GetMapping("listAllByTypeId")
    public R listAllByTypeId(@RequestParam("id") String id) {
        List<PlayClerkUserInfoEntity> list = playClerkUserInfoService.listAllByTypeId(id);
        return R.ok(list);
    }

    /**
     * 按照店员分组查询店员列表
     */
    @GetMapping("listAllByGroupId")
    public R listAllByGroupId(@RequestParam("id") String id) {
        List<PlayClerkUserInfoEntity> list = playClerkUserInfoService.listAllByGroupId(id);
        return R.ok(list);
    }

    /**
     * 修改店员分类
     */
    @PostMapping("editClerkType")
    public R listByPage(@Validated @RequestBody PlayClerkUserTypeEditVo vo) {
        //先清空当前分类下店员
        List<PlayClerkUserInfoEntity> list = playClerkUserInfoService.listAllByTypeId(vo.getTypeId());
        for (PlayClerkUserInfoEntity clerkUserInfo : list) {
            PlayClerkUserInfoEntity entity = new PlayClerkUserInfoEntity();
            entity.setId(clerkUserInfo.getId());
            entity.setTypeId("");
            playClerkUserInfoService.update(entity);
        }
        for (String clerkUserId : vo.getClerkUserIds()) {
            PlayClerkUserInfoEntity entity = new PlayClerkUserInfoEntity();
            entity.setId(clerkUserId);
            entity.setTypeId(vo.getTypeId());
            playClerkUserInfoService.update(entity);
        }

        return R.ok("成功");
    }


    /**
     * 1、修改店员分组
     * 2、将对应账号角色ID设为组长
     */
    @PostMapping("editClerkGroup")
    public R editClerkGroup(@Validated @RequestBody PlayClerkUserTypeEditVo vo) {
        //先清空当前分类下店员
        List<PlayClerkUserInfoEntity> list = playClerkUserInfoService.listAllByGroupId(vo.getTypeId());
        for (PlayClerkUserInfoEntity clerkUserInfo : list) {
            PlayClerkUserInfoEntity entity = new PlayClerkUserInfoEntity();
            entity.setId(clerkUserInfo.getId());
            entity.setGroupId("");
            playClerkUserInfoService.update(entity);
        }
        for (String clerkUserId : vo.getClerkUserIds()) {
            PlayClerkUserInfoEntity entity = new PlayClerkUserInfoEntity();
            entity.setId(clerkUserId);
            entity.setGroupId(vo.getTypeId());
            playClerkUserInfoService.update(entity);
        }


        return R.ok("成功");
    }


    @GetMapping("/simple/list")
    public R simpleList() {
        List<PlayClerkUserInfoEntity> clerkList = playClerkUserInfoService.simpleList();
        List<PlayCustomUserInfoEntity> customerList = customUserInfoService.simpleList();
        return R.ok(new JSONObject().fluentPut("clerkList", clerkList).fluentPut("customerList", customerList));
    }


    /**
     * 查询店员列表
     */
    @GetMapping("/list")
    public R list(PlayClerkUserInfoQueryVo vo) {
        IPage<PlayClerkUserInfoResultVo> list = playClerkUserInfoService.selectPlayClerkUserInfoByPage(vo);
        return R.ok(list);
    }

    /**
     * 获取店员详细信息
     */
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id) {
        return R.ok(playClerkUserInfoService.selectById(id));
    }

//
//    /**
//     * 微信端口新增店员
//     */
//    @Log(title = "店员", businessType = BusinessType.INSERT)
//    @PostMapping("/wx/add")
//    public R add(@Validated @RequestBody PlayClerkUserAddToWxVo vo) {
//        //微信申请成为店员，需要先创建账户。
//        PlayUserInfoEntity userInfoEntity = ConvertUtil.entityToVo(vo, PlayUserInfoEntity.class);
//        userInfoEntity.setId(IdUtils.getUuid());
//        playUserInfoService.create(userInfoEntity);
//        //账号创建完成后，创建店员
//        PlayClerkUserInfoEntity clerkUserInfoEntity = ConvertUtil.entityToVo(vo, PlayClerkUserInfoEntity.class);
//        clerkUserInfoEntity.setPlayUserId(userInfoEntity.getId());
//        boolean success = playClerkUserInfoService.create(clerkUserInfoEntity);
//        if (success) {
//            clerkCommodityService.initClerkCommodity(userInfoEntity.getId());
//            return R.ok();
//        }
//        return R.error("添加失败");
//    }


//    /**
//     * 新增店员
//     */
//    @Log(title = "店员", businessType = BusinessType.INSERT)
//    @PostMapping("/create")
//    public R create(@Validated @RequestBody PlayClerkUserAddVo vo) {
//        PlayClerkUserInfoEntity entity = ConvertUtil.entityToVo(vo, PlayClerkUserInfoEntity.class);
//        boolean success = playClerkUserInfoService.create(entity);
//        if (success) {
//            clerkCommodityService.initClerkCommodity(vo.getPlayUserId());
//            return R.ok();
//        }
//        return R.error("添加失败");
//    }

    /**
     * 修改店员
     */
    //@PreAuthorize("@customSs.hasPermission('clerk:user:update')")
    @Log(title = "店员", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public R update(@Validated @RequestBody PlayClerkUserEditVo vo) {
        PlayClerkUserInfoEntity entity = ConvertUtil.entityToVo(vo, PlayClerkUserInfoEntity.class);
        boolean success = playClerkUserInfoService.update(entity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }


    /**
     * 修改店员状态
     */
    //@PreAuthorize("@customSs.hasPermission('clerk:user:update')")
    @Log(title = "店员", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateState")
    public R updateState(@Validated @RequestBody PlayClerkUserStateEditVo vo) {
        PlayClerkUserInfoEntity entity = ConvertUtil.entityToVo(vo, PlayClerkUserInfoEntity.class);
        boolean success = playClerkUserInfoService.update(entity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除店员
     */
    //@PreAuthorize("@customSs.hasPermission('clerk:user:delete')")
    @Log(title = "店员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.ok(playClerkUserInfoService.deletePlayClerkUserInfoByIds(ids));
    }
}
