package com.zhangmeng.admin.manager.controller;

import com.github.pagehelper.PageInfo;
import com.zhangmeng.api.service.admin_manager.UserControllerApi;
import com.zhangmeng.model.base.baseController.BaseController;
import com.zhangmeng.model.base.baseUtil.CommonUtil;
import com.zhangmeng.model.dto.query.QueryParams;
import com.zhangmeng.model.entity.Role;
import com.zhangmeng.model.entity.User;
import com.zhangmeng.model.vo.Result;
import com.zhangmeng.model.vo.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import com.zhangmeng.admin.manager.service.UserService;

import java.util.Map;

/**
 * @author zhangmeng
 * @version 1.0
 * @date 2021年7月10日09:15:06
 */
@RestController
@RequestMapping("/user")
public  class UserController extends BaseController implements UserControllerApi {

    @Autowired
    private UserService userService;

    @Override
    @PostMapping("/save")
    public Result save(@RequestParam @RequestBody Map<String, Object> parms) {
        return this.userService.save_user(parms);
    }

    @Override
    @PostMapping("/delete")
    public Result delete(String userId) {
        if (userId != null && !userId.equals("")) {
            this.userService.deleteById(Long.parseLong(userId));
        }
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @Override
    @GetMapping("/list")
    public Result list(Integer pageNum, Integer pageSize, String username, String telephone) {
        Condition condition = new Condition(User.class);
        Example.Criteria criteria = condition.createCriteria();
        if (username != null && !username.equals("")) {
            criteria.andLike("username", "%" + username + "%");
        }
        if (telephone != null && !telephone.equals("")) {
            criteria.andLike("telephone", "%" + telephone + "%");
        }
        PageInfo<User> pageInfo = this.userService.findByCondition(new QueryParams(pageNum, pageSize, condition,"addTime desc"),true);
        return new Result(true, StatusCode.OK, "查询成功",pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    @PostMapping("/batchRemove")
    public Result batchRemove(String ids){
        if (CommonUtil.isNotNull(ids)){
            this.userService.deleteByIds(CommonUtil.firstLastComma(ids));
        }
        return new Result(true,StatusCode.OK,"批量删除成功");
    }

    @Override
    @PostMapping("/reg_save")
    public Result reg_save(@RequestParam @RequestBody Map<String, Object> parms) {
        return this.userService.reg_save(parms);
    }

    @Override
    @GetMapping("/current")
    public Result current(){
       return null;
    }

    @Override
    @PostMapping("/personUpdate")
    public Result personUpdate(@RequestParam @RequestBody Map<String,Object> map){
        User user = null;
        boolean flag = false;
        String userId = CommonUtil.map_get_value(map, "userId");
        if (userId != null && !userId.equals("")){
            user = this.userService.findById(Long.parseLong(userId));
            flag = true;
        }else {
            user = new User();
        }
        String username = CommonUtil.map_get_value(map, "username");
        if (CommonUtil.isNotNull(username)){
            user.setUsername(username);
        }

        String sex = CommonUtil.map_get_value(map, "sex");
        if (CommonUtil.isNotNull(sex)){
            user.setGender(CommonUtil.getEnum(sex,User.Gender.class));
        }
        String status = CommonUtil.map_get_value(map, "status");
        if (CommonUtil.isNotNull(status)){
            user.setStatus(CommonUtil.getEnum(status,User.Status.class));
        }

        String role_type = CommonUtil.map_get_value(map, "role_name");
        if (CommonUtil.isNotNull(role_type)){
            user.setRoleType(CommonUtil.getEnum(role_type, Role.Type.class));
        }

        String email = CommonUtil.map_get_value(map, "email");
        if (CommonUtil.isNotNull(email)){
            user.setEmail(email);
        }
        String telephone = CommonUtil.map_get_value(map, "telephone");
        if (CommonUtil.isNotNull(telephone)){
            user.setTelephone(telephone);
        }

        String profession = CommonUtil.map_get_value(map, "profession");
        if (CommonUtil.isNotNull(profession)){
            user.setProfession(profession);
        }

        String sign = CommonUtil.map_get_value(map, "sign");
        if (CommonUtil.isNotNull(sign)){
            user.setSign(sign);
        }

        String file_id = CommonUtil.map_get_value(map, "file_id");
        if (CommonUtil.isNotNull(file_id)){
            user.setPhoto_id(Long.parseLong(file_id));
        }
        String upload_image = CommonUtil.map_get_value(map, "upload_image");
        if (CommonUtil.isNotNull(upload_image)){
            user.setAvatar(upload_image);
        }
        if (flag){
            this.userService.update(user);
        }else {
            this.userService.save(user);
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @Override
    @GetMapping("/findByUserName")
    public User findByUserName(String username) {
        return this.userService.loadUserByUsername(username);
    }
}
