package com.jpsoft.shinestar.modules.base.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.entity.PersonRoleIcon;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.base.entity.PersonPopedomIcon;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomIconService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/base/personPopedomIcon")
@Api(description = "personPopedomIcon")
@Slf4j
public class PersonPopedomIconController {
    @Autowired
    private PersonPopedomIconService personPopedomIconService;

    @ApiOperation(value="创建空记录")
    @GetMapping("create")
    public MessageResult<PersonPopedomIcon> create(){
        MessageResult<PersonPopedomIcon> msgResult = new MessageResult<>();

        PersonPopedomIcon personPopedomIcon = new PersonPopedomIcon();

        msgResult.setData(personPopedomIcon);
        msgResult.setResult(true);

        return msgResult;
    }
    
    @ApiOperation(value="添加信息")
    @PostMapping("add")
    public MessageResult<PersonPopedomIcon> add(@RequestBody PersonPopedomIcon personPopedomIcon,@RequestAttribute String subject){
        MessageResult<PersonPopedomIcon> msgResult = new MessageResult<>();

        try {
            personPopedomIcon.setId(UUID.randomUUID().toString());
            personPopedomIcon.setDelFlag(false);
            personPopedomIcon.setCreateBy(subject);
            personPopedomIcon.setCreateTime(new Date());
            
            int affectCount = personPopedomIconService.insert(personPopedomIcon);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(personPopedomIcon);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            log.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="获取信息")
    @GetMapping("edit/{id}")
    public MessageResult<PersonPopedomIcon> edit(@PathVariable("id") String id){
        MessageResult<PersonPopedomIcon> msgResult = new MessageResult<>();

        try {
            PersonPopedomIcon personPopedomIcon = personPopedomIconService.get(id);

            if (personPopedomIcon != null) {
                msgResult.setResult(true);
                msgResult.setData(personPopedomIcon);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库不存在该记录!");
            }
        }
        catch(Exception ex){
            log.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="更新用户")
    @PostMapping("update")
    public MessageResult<PersonPopedomIcon> update(@RequestBody PersonPopedomIcon personPopedomIcon,@RequestAttribute String subject){
        MessageResult<PersonPopedomIcon> msgResult = new MessageResult<>();

        try {
            personPopedomIcon.setUpdateBy(subject);
            personPopedomIcon.setUpdateTime(new Date());
            
            int affectCount = personPopedomIconService.update(personPopedomIcon);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(personPopedomIcon);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            log.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

	@ApiOperation(value="删除")
    @PostMapping("delete/{id}")
    public MessageResult<Integer> delete(@PathVariable("id") String id,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            PersonPopedomIcon personPopedomIcon = personPopedomIconService.get(id);
            personPopedomIcon.setDelFlag(true);
            personPopedomIcon.setUpdateBy(subject);
            personPopedomIcon.setUpdateTime(new Date());

            int affectCount = personPopedomIconService.update(personPopedomIcon);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            log.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }


    @ApiOperation(value="批量删除")
    @PostMapping("batchDelete")
    public MessageResult<Integer> batchDelete(@RequestBody List<String> idList,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            int affectCount = 0;

            for (String id : idList) {
                PersonPopedomIcon personPopedomIcon = personPopedomIconService.get(id);
                personPopedomIcon.setDelFlag(true);
                personPopedomIcon.setUpdateBy(subject);
                personPopedomIcon.setUpdateTime(new Date());

                affectCount += personPopedomIconService.update(personPopedomIcon);
            }

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            log.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="列表")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            String id,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            @RequestAttribute String subject){

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("id_","asc"));

        if (StringUtils.isNotEmpty(id)) {
            searchParams.put("id","%" + id + "%");
        }

        Page<PersonPopedomIcon> page = personPopedomIconService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }

    @GetMapping("findRelatedIcon")
    @ApiOperation(value="")
    public MessageResult<List> findRelatedIcon(String personPopedomId){
        MessageResult<List> messageResult = new MessageResult<>();

        try {
             List<PersonPopedomIcon> list = personPopedomIconService.findByPersonPopedomId(personPopedomId);

            //todo 填写具体代码
            messageResult.setData(list);
            messageResult.setResult(true);
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);

            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

    @PostMapping("saveRelatedIcon")
    @ApiOperation(value="保存分配菜单")
    public MessageResult<Integer> saveRelatedIcon(String personPopedomId,String idList,@RequestAttribute String subject){
        MessageResult<Integer> messageResult = new MessageResult<>();

        try {
            personPopedomIconService.deleteByPersonPopedomId(personPopedomId);

            String[] idArr = idList.split(",");

            int affectCount = 0;

            Date now = new Date();

            for (String iconId : idArr) {
                PersonPopedomIcon personPopedomIcon = new PersonPopedomIcon();
                personPopedomIcon.setId(UUID.randomUUID().toString());
                personPopedomIcon.setPersonPopedomId(personPopedomId);
                personPopedomIcon.setIconId(iconId);
                personPopedomIcon.setCreateBy(subject);
                personPopedomIcon.setCreateTime(now);
                personPopedomIcon.setDelFlag(false);

                affectCount += personPopedomIconService.insert(personPopedomIcon);

                //创建时间顺延100毫秒
                now = new Date(now.getTime() + 100);
            }

            //todo 填写具体代码
            messageResult.setData(affectCount);
            messageResult.setResult(true);
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);

            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }
}
