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

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.IconInfo;
import com.jpsoft.shinestar.modules.base.entity.IconType;
import com.jpsoft.shinestar.modules.base.entity.PersonRoleIcon;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.base.service.IconInfoService;
import com.jpsoft.shinestar.modules.base.service.IconTypeService;
import com.jpsoft.shinestar.modules.base.service.PersonRoleIconService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 java.util.*;

/**
 * 图标信息表
 * sz
 * **/
@RestController
@RequestMapping("/base/iconInfo")
@Api(description = "iconInfo")
public class IconInfoController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IconInfoService iconInfoService;
    @Autowired
    private IconTypeService iconTypeService;
    @Autowired
    private PersonRoleIconService personRoleIconService;


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

        IconInfo iconInfo = new IconInfo();

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

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

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

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

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

        return msgResult;
    }

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

        try {
            IconInfo iconInfo = iconInfoService.get(id);

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

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

        return msgResult;
    }

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

        try {
            iconInfo.setUpdateBy(subject);
            iconInfo.setUpdateTime(new Date());
            
            int affectCount = iconInfoService.update(iconInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(iconInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            logger.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 {
            IconInfo iconInfo = iconInfoService.get(id);
            iconInfo.setDelFlag(true);
            iconInfo.setUpdateBy(subject);
            iconInfo.setUpdateTime(new Date());

            int affectCount = iconInfoService.update(iconInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.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) {
                IconInfo iconInfo = iconInfoService.get(id);
                iconInfo.setDelFlag(true);
                iconInfo.setUpdateBy(subject);
                iconInfo.setUpdateTime(new Date());

                affectCount += iconInfoService.update(iconInfo);
            }

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.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 iconName, String remark,
            @RequestParam(value="sceneId",defaultValue="") String sceneId,
            @RequestParam(value="useEnable",defaultValue="") String useEnable,
            @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("create_time","asc"));

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

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

        Page<IconInfo> page = iconInfoService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);
        for(IconInfo iconInfo : page.getResult()){
            if(iconInfo.getUseEnable()){
                iconInfo.setUseEnableName("已启用");
            }
            else{
                iconInfo.setUseEnableName("未启用");
            }

            if(StringUtils.isNotEmpty(iconInfo.getIconTypeId())) {
                IconType iconType = iconTypeService.get(iconInfo.getIconTypeId());
                iconInfo.setIconTypeName(iconType.getTypeName());
            }
        }

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

        return msgResult;
    }

    @ApiOperation(value="根据角色ID去角色图标关系表查")
    @RequestMapping(value = "listByRoleId",method = RequestMethod.POST)
    public MessageResult<List<PersonRoleIcon>> listByCompanyId(String roleId, @RequestAttribute String subject){
        MessageResult<List<PersonRoleIcon>> msgResult = new MessageResult<>();
        List<PersonRoleIcon> list = personRoleIconService.findByPersonRoleId(roleId);
        msgResult.setResult(true);
        msgResult.setData(list);
        return msgResult;
    }

    @ApiOperation(value="全部图标类型列表")
    @RequestMapping(value = "getIconTypeList",method = RequestMethod.POST)
    public MessageResult<List> getIconTypeList(){
        MessageResult<List> msgResult = new MessageResult<>();
        List<IconType> list = iconTypeService.list();
        msgResult.setResult(true);
        msgResult.setData(list);
        return msgResult;
    }

    @ApiOperation(value="所有图标列表")
    @RequestMapping(value = "list",method = RequestMethod.POST)
    public MessageResult<List> list(){
        MessageResult<List> msgResult = new MessageResult<>();
        List<IconInfo> list = iconInfoService.list();

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

        return msgResult;
    }
}
